3 #include "utf8_strings.h"
5 void litehtml::trim(string
&s
, const string
& chars_to_trim
)
7 string::size_type pos
= s
.find_first_not_of(chars_to_trim
);
8 if(pos
!= string::npos
)
10 s
.erase(s
.begin(), s
.begin() + pos
);
17 pos
= s
.find_last_not_of(chars_to_trim
);
18 if(pos
!= string::npos
)
20 s
.erase(s
.begin() + pos
+ 1, s
.end());
24 void litehtml::lcase(string
&s
)
32 litehtml::string::size_type
litehtml::find_close_bracket(const string
&s
, string::size_type off
, char open_b
, char close_b
)
35 for(string::size_type i
= off
; i
< s
.length(); i
++)
40 } else if(s
[i
] == close_b
)
52 litehtml::string
litehtml::index_value(int index
, const string
& strings
, char delim
)
54 std::vector
<string
> vals
;
56 delims
.push_back(delim
);
57 split_string(strings
, vals
, delims
);
58 if(index
>= 0 && index
< vals
.size())
62 return std::to_string(index
);
65 int litehtml::value_index( const string
& val
, const string
& strings
, int defValue
, char delim
)
67 if(val
.empty() || strings
.empty() || !delim
)
73 string::size_type delim_start
= 0;
74 string::size_type delim_end
= strings
.find(delim
, delim_start
);
75 string::size_type item_len
;
78 if(delim_end
== string::npos
)
80 item_len
= strings
.length() - delim_start
;
83 item_len
= delim_end
- delim_start
;
85 if(item_len
== val
.length())
87 if(val
== strings
.substr(delim_start
, item_len
))
93 delim_start
= delim_end
;
94 if(delim_start
== string::npos
) break;
96 if(delim_start
== strings
.length()) break;
97 delim_end
= strings
.find(delim
, delim_start
);
102 bool litehtml::value_in_list( const string
& val
, const string
& strings
, char delim
)
104 int idx
= value_index(val
, strings
, -1, delim
);
112 void litehtml::split_string(const string
& str
, string_vector
& tokens
, const string
& delims
, const string
& delims_preserve
, const string
& quote
)
114 if(str
.empty() || (delims
.empty() && delims_preserve
.empty()))
119 string all_delims
= delims
+ delims_preserve
+ quote
;
121 string::size_type token_start
= 0;
122 string::size_type token_end
= str
.find_first_of(all_delims
, token_start
);
123 string::size_type token_len
;
127 while( token_end
!= string::npos
&& quote
.find_first_of(str
[token_end
]) != string::npos
)
129 if(str
[token_end
] == '(')
131 token_end
= find_close_bracket(str
, token_end
, '(', ')');
132 } else if(str
[token_end
] == '[')
134 token_end
= find_close_bracket(str
, token_end
, '[', ']');
135 } else if(str
[token_end
] == '{')
137 token_end
= find_close_bracket(str
, token_end
, '{', '}');
140 token_end
= str
.find_first_of(str
[token_end
], token_end
+ 1);
142 if(token_end
!= string::npos
)
144 token_end
= str
.find_first_of(all_delims
, token_end
+ 1);
148 if(token_end
== string::npos
)
150 token_len
= string::npos
;
153 token_len
= token_end
- token_start
;
156 token
= str
.substr(token_start
, token_len
);
159 tokens
.push_back( token
);
161 if(token_end
!= string::npos
&& !delims_preserve
.empty() && delims_preserve
.find_first_of(str
[token_end
]) != string::npos
)
163 tokens
.push_back( str
.substr(token_end
, 1) );
166 token_start
= token_end
;
167 if(token_start
== string::npos
) break;
169 if(token_start
== str
.length()) break;
170 token_end
= str
.find_first_of(all_delims
, token_start
);
174 void litehtml::join_string(string
& str
, const string_vector
& tokens
, const string
& delims
)
177 for (size_t i
= 0; i
< tokens
.size(); i
++)
187 int litehtml::t_strcasecmp(const char *s1
, const char *s2
)
193 c
= t_tolower((unsigned char)s1
[i
]);
194 d
= c
- t_tolower((unsigned char)s2
[i
]);
204 int litehtml::t_strncasecmp(const char *s1
, const char *s2
, size_t n
)
208 for (i
= 0; i
< n
; i
++)
210 c
= t_tolower((unsigned char)s1
[i
]);
211 d
= c
- t_tolower((unsigned char)s2
[i
]);
223 litehtml::string
litehtml::get_escaped_string(const string
& in_str
)
226 for (auto ch
: in_str
)
281 bool litehtml::is_number(const string
& string
, const bool allow_dot
) {
282 for (auto ch
: string
)
284 if (!(t_isdigit(ch
) || (allow_dot
&& ch
== '.')))