Skip to content

Commit aa98991

Browse files
committed
fix
1 parent a1be65d commit aa98991

2 files changed

Lines changed: 40 additions & 25 deletions

File tree

include/cpp-terminfo/Type.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@ class Type
1919
{
2020
public:
2121
Type() = default;
22-
Type(const std::vector<std::string>& aliases, const std::string& description) : m_description(description), m_aliases(aliases)
22+
Type(const std::vector<std::string>& aliases, const std::string& description) : m_aliases(aliases), m_description(description)
2323
{
2424
m_description.shrink_to_fit();
2525
for(std::vector<std::string>::iterator it = m_aliases.begin(); it != m_aliases.end(); ++it) it->shrink_to_fit();
2626
}
27+
Type(const std::vector<std::string>& aliases) : m_aliases(aliases)
28+
{
29+
for(std::vector<std::string>::iterator it = m_aliases.begin(); it != m_aliases.end(); ++it) it->shrink_to_fit();
30+
}
2731
std::string description() const noexcept { return m_description; }
2832
std::string name() const noexcept { return m_aliases[0]; }
2933
bool hasAlias() const noexcept { return m_aliases.size() - 1 != 0; }

src/Parser.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,33 @@
1919

2020
static std::vector<std::string> split(const std::string& s, const char& delimiter, const char& annuler = '\\')
2121
{
22-
if(s.empty()) return {};
23-
std::vector<std::string> tokens;
24-
std::size_t pos_begin{0};
25-
std::size_t pos_last{0};
26-
std::size_t skip{0};
27-
while((pos_last = s.find(delimiter, skip)) != std::string::npos)
28-
{
29-
if(pos_last - 1 >= 0 && s[pos_last - 1] != annuler)
22+
if (s.empty()) return {}; // Handle empty string
23+
24+
std::vector<std::string> tokens;
25+
std::size_t pos_begin = 0;
26+
std::size_t pos_last = 0;
27+
std::size_t skip = 0;
28+
29+
while ((pos_last = s.find(delimiter, skip)) != std::string::npos)
3030
{
31-
tokens.push_back(s.substr(pos_begin, pos_last - pos_begin));
32-
pos_begin = pos_last + 1;
33-
skip = pos_last + 1;
31+
// If delimiter is not escaped, push token
32+
if (pos_last > 0 && s[pos_last - 1] != annuler)
33+
{
34+
tokens.push_back(s.substr(pos_begin, pos_last - pos_begin));
35+
pos_begin = pos_last + 1;
36+
skip = pos_last + 1;
37+
}
38+
else
39+
{
40+
// Skip escaped delimiter
41+
++skip;
42+
}
3443
}
35-
else if(pos_last == 0)
36-
tokens.push_back("");
37-
else
38-
++skip;
39-
}
40-
if(pos_last != s.size() - 1) tokens.push_back(s.substr(pos_begin, s.size() - pos_begin));
41-
return tokens;
44+
45+
// Add the last token, including empty ones if delimiter is at the end
46+
tokens.push_back(s.substr(pos_begin));
47+
48+
return tokens;
4249
}
4350

4451
void Terminfo::Parser::parse()
@@ -62,14 +69,14 @@ void Terminfo::Parser::parse()
6269
}
6370
case '\t':
6471
{
65-
std::string line(5000, '\0');
72+
std::string line(82, '\0');
6673
std::getline(fs, line);
6774
capabilities_lines += line;
6875
break;
6976
}
7077
default:
7178
{
72-
std::string line(5000, '\0');
79+
std::string line(82, '\0');
7380
std::getline(fs, line);
7481
parseCapabilities(capabilities_lines);
7582
parseType(line);
@@ -92,10 +99,14 @@ void Terminfo::Parser::parse()
9299
void Terminfo::Parser::parseType(const std::string& line)
93100
{
94101
std::vector<std::string> tokens = split(line, '|');
95-
std::string description{tokens[tokens.size() - 1]}; // Description
96-
description = description.substr(0, description.size() - 1); // Suppress the ,
97-
tokens.pop_back();
98-
m_infos.push_back(Terminfo({tokens, description}));
102+
if(tokens.size()>1)
103+
{
104+
std::string description{tokens[tokens.size() - 1]}; // Description
105+
description = description.substr(0, description.size() - 1); // Suppress the ,
106+
tokens.pop_back();
107+
m_infos.push_back(Terminfo({tokens, description}));
108+
}
109+
else m_infos.push_back(Terminfo(tokens));
99110
}
100111

101112
void Terminfo::Parser::parseCapabilities(std::string& line)

0 commit comments

Comments
 (0)