1919
2020static 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
4451void 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()
9299void 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
101112void Terminfo::Parser::parseCapabilities (std::string& line)
0 commit comments