string.hpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  1. /*
  2. * Copyright (c) 2017-2023 zhllxt
  3. *
  4. * author : zhllxt
  5. * qq : 37792738
  6. * email : 37792738@qq.com
  7. *
  8. * https://github.com/Shot511/strutil
  9. *
  10. */
  11. #ifndef __ASIO2_STRING_HPP__
  12. #define __ASIO2_STRING_HPP__
  13. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #pragma once
  15. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  16. #include <asio2/base/detail/push_options.hpp>
  17. #if defined(__GNUC__) || defined(__GNUG__)
  18. # pragma GCC diagnostic ignored "-Warray-bounds"
  19. #endif
  20. #include <cstdint>
  21. #include <cstdarg>
  22. #include <cstdio>
  23. #include <cwchar>
  24. #include <climits>
  25. #include <cctype>
  26. #include <cstring>
  27. #include <string>
  28. #include <string_view>
  29. #include <vector>
  30. #include <type_traits>
  31. #include <algorithm>
  32. #include <sstream>
  33. #include <regex>
  34. #include <map>
  35. #include <asio2/base/detail/type_traits.hpp>
  36. namespace asio2
  37. {
  38. /**
  39. * @brief Converts any datatype into std::basic_string.
  40. * @tparam T
  41. * @param v - will be converted into std::basic_string.
  42. * @return Converted value as std::basic_string.
  43. */
  44. template<typename T>
  45. inline auto to_basic_string(T&& v)
  46. {
  47. using type = typename detail::remove_cvref_t<T>;
  48. using CharT = typename detail::char_type<type>::type;
  49. if /**/ constexpr (detail::is_string_view_v<type>)
  50. {
  51. return std::basic_string<CharT>{ v.data(), v.size() };
  52. }
  53. else if constexpr (detail::is_string_v<type>)
  54. {
  55. return std::forward<T>(v);
  56. }
  57. else if constexpr (detail::is_char_v<type>)
  58. {
  59. return std::basic_string<CharT>{ 1, v };
  60. }
  61. else if constexpr (std::is_integral_v<type>)
  62. {
  63. std::basic_string<CharT> r;
  64. std::string s = std::to_string(v);
  65. for (auto c : s)
  66. {
  67. r += static_cast<CharT>(c);
  68. }
  69. return r;
  70. }
  71. else if constexpr (std::is_floating_point_v<type>)
  72. {
  73. std::basic_string<CharT> r;
  74. std::string s = std::to_string(v);
  75. for (auto c : s)
  76. {
  77. r += static_cast<CharT>(c);
  78. }
  79. return r;
  80. }
  81. else if constexpr (detail::is_char_pointer_v<type>)
  82. {
  83. if (v)
  84. return std::basic_string<CharT>{ v };
  85. else
  86. return std::basic_string<CharT>{ };
  87. }
  88. else if constexpr (detail::is_char_array_v<type>)
  89. {
  90. return std::basic_string<CharT>{ reinterpret_cast<const CharT*>(v) };
  91. }
  92. else
  93. {
  94. std::basic_stringstream<CharT> ss;
  95. ss << std::forward<T>(v);
  96. return ss.str();
  97. }
  98. }
  99. /**
  100. * @brief Converts any datatype into std::basic_string_view.
  101. * @tparam T
  102. * @param v - will be converted into std::basic_string_view.
  103. * @return Converted value as std::basic_string_view.
  104. */
  105. template<typename T>
  106. inline auto to_basic_string_view(const T& v)
  107. {
  108. using type = typename detail::remove_cvref_t<T>;
  109. using CharT = typename detail::char_type<type>::type;
  110. if /**/ constexpr (detail::is_string_view_v<type>)
  111. {
  112. return v;
  113. }
  114. else if constexpr (detail::is_string_v<type>)
  115. {
  116. return std::basic_string_view<CharT>{ v };
  117. }
  118. else if constexpr (detail::is_char_v<type>)
  119. {
  120. return std::basic_string_view<CharT>{ std::addressof(v), 1 };
  121. }
  122. else if constexpr (detail::is_char_pointer_v<type>)
  123. {
  124. return (v ? std::basic_string_view<CharT>{ v } : std::basic_string_view<CharT>{});
  125. }
  126. else if constexpr (detail::is_char_array_v<type>)
  127. {
  128. return std::basic_string_view<CharT>{ reinterpret_cast<const CharT*>(v) };
  129. }
  130. else
  131. {
  132. return std::basic_string_view<CharT>{ v };
  133. }
  134. }
  135. /**
  136. * @brief Converts any datatype into std::string.
  137. * @tparam T
  138. * @param v - will be converted into std::string.
  139. * @return Converted value as std::string.
  140. */
  141. template<typename T>
  142. inline std::string to_string(T&& v)
  143. {
  144. using type = detail::remove_cvref_t<T>;
  145. std::string s;
  146. if /**/ constexpr (std::is_same_v<std::string_view, type>)
  147. {
  148. s = { v.data(), v.size() };
  149. }
  150. else if constexpr (std::is_same_v<std::string, type>)
  151. {
  152. s = std::forward<T>(v);
  153. }
  154. else if constexpr (std::is_integral_v<type>)
  155. {
  156. s = std::to_string(v);
  157. }
  158. else if constexpr (std::is_floating_point_v<type>)
  159. {
  160. s = std::to_string(v);
  161. }
  162. else if constexpr (detail::is_char_pointer_v<type>)
  163. {
  164. if (v) s = v;
  165. }
  166. else if constexpr (detail::is_char_array_v<type>)
  167. {
  168. s = std::forward<T>(v);
  169. }
  170. else
  171. {
  172. std::stringstream ss;
  173. ss << std::forward<T>(v);
  174. s = ss.str();
  175. }
  176. return s;
  177. }
  178. /**
  179. * @brief Converts any datatype into std::string_view.
  180. * @tparam T
  181. * @param v - will be converted into std::string_view.
  182. * @return Converted value as std::string_view.
  183. */
  184. template<typename T>
  185. inline std::string_view to_string_view(const T& v)
  186. {
  187. using type = detail::remove_cvref_t<T>;
  188. if /**/ constexpr (std::is_same_v<std::string_view, type>)
  189. {
  190. return std::string_view{ v };
  191. }
  192. else if constexpr (std::is_same_v<std::string, type>)
  193. {
  194. return std::string_view{ v };
  195. }
  196. else if constexpr (detail::is_char_v<type>)
  197. {
  198. return std::string_view{ std::addressof(v), 1 };
  199. }
  200. else if constexpr (detail::is_char_pointer_v<type>)
  201. {
  202. return (v ? std::string_view{ v } : std::string_view{});
  203. }
  204. else if constexpr (detail::is_char_array_v<type>)
  205. {
  206. return std::string_view{ v };
  207. }
  208. else
  209. {
  210. return std::string_view{ v };
  211. }
  212. }
  213. /**
  214. * @brief Converts iterator range into std::string_view.
  215. * @tparam T
  216. * @param v - will be converted into std::string_view.
  217. * @return Converted value as std::string_view.
  218. */
  219. template<typename Iterator>
  220. inline std::string_view to_string_view(const Iterator& first, const Iterator& last)
  221. {
  222. using iter_type = typename detail::remove_cvref_t<Iterator>;
  223. using diff_type = typename std::iterator_traits<iter_type>::difference_type;
  224. diff_type n = std::distance(first, last);
  225. if (n <= static_cast<diff_type>(0))
  226. {
  227. return std::string_view{};
  228. }
  229. if constexpr (std::is_pointer_v<iter_type>)
  230. {
  231. return { first, static_cast<std::string_view::size_type>(n) };
  232. }
  233. else
  234. {
  235. return { first.operator->(), static_cast<std::string_view::size_type>(n) };
  236. }
  237. }
  238. /**
  239. * @brief Converts any datatype into a numeric.
  240. * @tparam IntegerType - integer or floating
  241. * @param v - will be converted into numeric.
  242. * @return Converted value as numeric.
  243. */
  244. template<typename IntegerType, typename T>
  245. inline IntegerType to_numeric(T&& v) noexcept
  246. {
  247. using type = detail::remove_cvref_t<T>;
  248. if /**/ constexpr (std::is_integral_v<type>)
  249. {
  250. return static_cast<IntegerType>(v);
  251. }
  252. else if constexpr (std::is_floating_point_v<type>)
  253. {
  254. return static_cast<IntegerType>(v);
  255. }
  256. else
  257. {
  258. std::string s = asio2::to_string(std::forward<T>(v));
  259. int rx = 10;
  260. if (s.size() >= std::size_t(2) && s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
  261. rx = 16;
  262. return static_cast<IntegerType>(std::strtoull(s.data(), nullptr, rx));
  263. }
  264. }
  265. /**
  266. * @brief Converts std::string into any datatype.
  267. * Datatype must support << operator.
  268. * @tparam T
  269. * @param str - std::string that will be converted into datatype T.
  270. * @return Variable of datatype T.
  271. */
  272. template<typename T>
  273. inline T string_to(const std::string& str)
  274. {
  275. T result{};
  276. std::istringstream(str) >> result;
  277. return result;
  278. }
  279. /**
  280. * @brief Returns `true` if two strings are equal, using a case-insensitive comparison.
  281. */
  282. template<
  283. class CharT,
  284. class Traits = std::char_traits<CharT>
  285. >
  286. inline bool iequals(
  287. std::basic_string_view<CharT, Traits> str1,
  288. std::basic_string_view<CharT, Traits> str2) noexcept
  289. {
  290. auto n = str1.size();
  291. if (str2.size() != n)
  292. return false;
  293. auto p1 = str1.data();
  294. auto p2 = str2.data();
  295. CharT a, b;
  296. // fast loop
  297. while (n--)
  298. {
  299. a = *p1++;
  300. b = *p2++;
  301. if (a != b)
  302. goto slow;
  303. }
  304. return true;
  305. slow:
  306. do
  307. {
  308. if (std::tolower(a) != std::tolower(b))
  309. return false;
  310. a = *p1++;
  311. b = *p2++;
  312. } while (n--);
  313. return true;
  314. }
  315. /**
  316. * @brief Returns `true` if two strings are equal, using a case-insensitive comparison.
  317. */
  318. template<class String1, class String2>
  319. inline bool iequals(const String1& str1, const String2& str2) noexcept
  320. {
  321. return asio2::iequals(asio2::to_basic_string_view(str1), asio2::to_basic_string_view(str2));
  322. }
  323. /**
  324. * @brief Compares two std::strings ignoring their case (lower/upper).
  325. * @param str1 - string to compare
  326. * @param str2 - string to compare
  327. * @return True if str1 and str2 are equal, false otherwise.
  328. */
  329. template<class String1, class String2>
  330. inline bool compare_ignore_case(const String1& str1, const String2& str2)
  331. {
  332. return asio2::iequals(str1, str2);
  333. }
  334. /**
  335. * std::string format
  336. */
  337. inline std::string formatv(const char * format, va_list args)
  338. {
  339. std::string str;
  340. if (format && *format)
  341. {
  342. // under windows and linux system,std::vsnprintf(nullptr, 0, format, args)
  343. // can get the need buffer len for the output,
  344. va_list args_copy;
  345. va_copy(args_copy, args);
  346. int len = std::vsnprintf(nullptr, 0, format, args_copy);
  347. if (len > 0)
  348. {
  349. str.resize(len);
  350. va_copy(args_copy, args);
  351. std::vsprintf((char*)str.data(), format, args_copy);
  352. }
  353. }
  354. return str;
  355. }
  356. /**
  357. * std::wstring format
  358. */
  359. inline std::wstring formatv(const wchar_t * format, va_list args)
  360. {
  361. std::wstring str;
  362. if (format && *format)
  363. {
  364. va_list args_copy;
  365. while (true)
  366. {
  367. str.resize(str.capacity());
  368. va_copy(args_copy, args);
  369. // if provided buffer size is less than required size,vswprintf will return -1
  370. // so if len equal -1,we increase the buffer size again, and has to use a loop
  371. // to get the correct output buffer len,
  372. int len = std::vswprintf((wchar_t*)(&str[0]), str.size(), format, args_copy);
  373. if (len == -1)
  374. str.reserve(str.capacity() * 2);
  375. else
  376. {
  377. str.resize(len);
  378. break;
  379. }
  380. }
  381. }
  382. return str;
  383. }
  384. /**
  385. * std::string format
  386. */
  387. inline std::string format(const char * format, ...)
  388. {
  389. std::string str;
  390. if (format && *format)
  391. {
  392. // under windows and linux system,std::vsnprintf(nullptr, 0, format, args)
  393. // can get the need buffer len for the output,
  394. va_list args;
  395. va_start(args, format);
  396. str = formatv(format, args);
  397. va_end(args);
  398. }
  399. return str;
  400. }
  401. /**
  402. * std::wstring format
  403. */
  404. inline std::wstring format(const wchar_t * format, ...)
  405. {
  406. std::wstring str;
  407. if (format && *format)
  408. {
  409. va_list args;
  410. va_start(args, format);
  411. str = formatv(format, args);
  412. va_end(args);
  413. }
  414. return str;
  415. }
  416. /**
  417. * @brief Converts string to lower case.
  418. * @param str - string that needs to be converted.
  419. * @return Lower case input string.
  420. */
  421. template<
  422. class CharT,
  423. class Traits = std::char_traits<CharT>,
  424. class Allocator = std::allocator<CharT>
  425. >
  426. inline std::basic_string<CharT, Traits, Allocator>& to_lower(std::basic_string<CharT, Traits, Allocator>& str)
  427. {
  428. std::transform(str.begin(), str.end(), str.begin(), [](CharT c) -> CharT
  429. {
  430. return static_cast<CharT>(std::tolower(c));
  431. });
  432. return str;
  433. }
  434. /**
  435. * @brief Converts string to lower case.
  436. * @param str - string that needs to be converted.
  437. * @return Lower case input string.
  438. */
  439. template<
  440. class CharT,
  441. class Traits = std::char_traits<CharT>,
  442. class Allocator = std::allocator<CharT>
  443. >
  444. inline std::basic_string<CharT, Traits, Allocator> to_lower(std::basic_string<CharT, Traits, Allocator>&& str)
  445. {
  446. std::transform(str.begin(), str.end(), str.begin(), [](CharT c) -> CharT
  447. {
  448. return static_cast<CharT>(std::tolower(c));
  449. });
  450. return std::move(str);
  451. }
  452. /**
  453. * @brief Converts string to upper case.
  454. * @param str - string that needs to be converted.
  455. * @return Upper case input string.
  456. */
  457. template<
  458. class CharT,
  459. class Traits = std::char_traits<CharT>,
  460. class Allocator = std::allocator<CharT>
  461. >
  462. inline std::basic_string<CharT, Traits, Allocator>& to_upper(std::basic_string<CharT, Traits, Allocator>& str)
  463. {
  464. std::transform(str.begin(), str.end(), str.begin(), [](CharT c) -> CharT
  465. {
  466. return static_cast<CharT>(std::toupper(c));
  467. });
  468. return str;
  469. }
  470. /**
  471. * @brief Converts string to upper case.
  472. * @param str - string that needs to be converted.
  473. * @return Upper case input string.
  474. */
  475. template<
  476. class CharT,
  477. class Traits = std::char_traits<CharT>,
  478. class Allocator = std::allocator<CharT>
  479. >
  480. inline std::basic_string<CharT, Traits, Allocator> to_upper(std::basic_string<CharT, Traits, Allocator>&& str)
  481. {
  482. std::transform(str.begin(), str.end(), str.begin(), [](CharT c) -> CharT
  483. {
  484. return static_cast<CharT>(std::toupper(c));
  485. });
  486. return std::move(str);
  487. }
  488. /**
  489. * @brief Converts the first character of a string to uppercase letter and lowercases all other characters, if any.
  490. * @param str - input string to be capitalized.
  491. * @return A string with the first letter capitalized and all other characters lowercased.
  492. */
  493. template<
  494. class CharT,
  495. class Traits = std::char_traits<CharT>,
  496. class Allocator = std::allocator<CharT>
  497. >
  498. inline std::basic_string<CharT, Traits, Allocator>& capitalize(std::basic_string<CharT, Traits, Allocator>& str)
  499. {
  500. asio2::to_lower(str);
  501. if (!str.empty())
  502. {
  503. str.front() = static_cast<CharT>(std::toupper(str.front()));
  504. }
  505. return str;
  506. }
  507. /**
  508. * @brief Converts the first character of a string to uppercase letter and lowercases all other characters, if any.
  509. * @param str - input string to be capitalized.
  510. * @return A string with the first letter capitalized and all other characters lowercased.
  511. */
  512. template<
  513. class CharT,
  514. class Traits = std::char_traits<CharT>,
  515. class Allocator = std::allocator<CharT>
  516. >
  517. inline std::basic_string<CharT, Traits, Allocator> capitalize(std::basic_string<CharT, Traits, Allocator>&& str)
  518. {
  519. asio2::to_lower(str);
  520. if (!str.empty())
  521. {
  522. str.front() = static_cast<CharT>(std::toupper(str.front()));
  523. }
  524. return std::move(str);
  525. }
  526. /**
  527. * @brief Converts only the first character of a string to uppercase letter, all other characters stay unchanged.
  528. * @param str - input string to be modified.
  529. * @return A string with the first letter capitalized. All other characters stay unchanged.
  530. */
  531. template<
  532. class CharT,
  533. class Traits = std::char_traits<CharT>,
  534. class Allocator = std::allocator<CharT>
  535. >
  536. inline std::basic_string<CharT, Traits, Allocator>& capitalize_first_char(
  537. std::basic_string<CharT, Traits, Allocator>& str)
  538. {
  539. if (!str.empty())
  540. {
  541. str.front() = static_cast<CharT>(std::toupper(str.front()));
  542. }
  543. return str;
  544. }
  545. /**
  546. * @brief Converts only the first character of a string to uppercase letter, all other characters stay unchanged.
  547. * @param str - input string to be modified.
  548. * @return A string with the first letter capitalized. All other characters stay unchanged.
  549. */
  550. template<
  551. class CharT,
  552. class Traits = std::char_traits<CharT>,
  553. class Allocator = std::allocator<CharT>
  554. >
  555. inline std::basic_string<CharT, Traits, Allocator> capitalize_first_char(
  556. std::basic_string<CharT, Traits, Allocator>&& str)
  557. {
  558. if (!str.empty())
  559. {
  560. str.front() = static_cast<CharT>(std::toupper(str.front()));
  561. }
  562. return std::move(str);
  563. }
  564. /**
  565. * @brief Checks if input string str contains specified substring.
  566. * @param str - string to be checked.
  567. * @param substring - searched substring or character.
  568. * @return True if substring or character was found in str, false otherwise.
  569. */
  570. template<
  571. class CharT,
  572. class Traits = std::char_traits<CharT>
  573. >
  574. inline bool contains(std::basic_string_view<CharT, Traits> str, std::basic_string_view<CharT, Traits> substring)
  575. {
  576. return str.find(substring) != std::string_view::npos;
  577. }
  578. /**
  579. * @brief Checks if input string str contains specified substring.
  580. * @param str - string to be checked.
  581. * @param substring - searched substring or character.
  582. * @return True if substring was found in str, false otherwise.
  583. */
  584. template<class String1, class String2>
  585. inline bool contains(const String1& str, const String2& substring)
  586. {
  587. return asio2::contains(asio2::to_basic_string_view(str), asio2::to_basic_string_view(substring));
  588. }
  589. /**
  590. * @brief trim each space character of the string: space \t \r \n and so on
  591. */
  592. template<
  593. class CharT,
  594. class Traits = std::char_traits<CharT>,
  595. class Allocator = std::allocator<CharT>
  596. >
  597. inline std::basic_string<CharT, Traits, Allocator>& trim_all(std::basic_string<CharT, Traits, Allocator>& str)
  598. {
  599. // https://zh.cppreference.com/w/cpp/algorithm/remove
  600. str.erase(std::remove_if(str.begin(), str.end(), [](int x) {return std::isspace(x); }), str.end());
  601. return str;
  602. }
  603. /**
  604. * @brief trim each space character of the string: space \t \r \n and so on
  605. */
  606. template<
  607. class CharT,
  608. class Traits = std::char_traits<CharT>,
  609. class Allocator = std::allocator<CharT>
  610. >
  611. inline std::basic_string<CharT, Traits, Allocator> trim_all(std::basic_string<CharT, Traits, Allocator>&& str)
  612. {
  613. // https://zh.cppreference.com/w/cpp/algorithm/remove
  614. str.erase(std::remove_if(str.begin(), str.end(), [](int x) {return std::isspace(x); }), str.end());
  615. return std::move(str);
  616. }
  617. /**
  618. * @brief trim left space character of the string: space \t \r \n and so on
  619. */
  620. template<
  621. class CharT,
  622. class Traits = std::char_traits<CharT>,
  623. class Allocator = std::allocator<CharT>
  624. >
  625. inline std::basic_string<CharT, Traits, Allocator>& trim_left(std::basic_string<CharT, Traits, Allocator>& str)
  626. {
  627. str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch) { return !std::isspace(ch); }));
  628. return str;
  629. }
  630. /**
  631. * @brief trim left space character of the string: space \t \r \n and so on
  632. */
  633. template<
  634. class CharT,
  635. class Traits = std::char_traits<CharT>,
  636. class Allocator = std::allocator<CharT>
  637. >
  638. inline std::basic_string<CharT, Traits, Allocator> trim_left(std::basic_string<CharT, Traits, Allocator>&& str)
  639. {
  640. str.erase(str.begin(), std::find_if(str.begin(), str.end(), [](int ch) { return !std::isspace(ch); }));
  641. return std::move(str);
  642. }
  643. /**
  644. * @brief trim left space character of the string: space \t \r \n and so on
  645. */
  646. template<
  647. class CharT,
  648. class Traits = std::char_traits<CharT>,
  649. class Allocator = std::allocator<CharT>
  650. >
  651. inline std::basic_string<CharT, Traits, Allocator>& ltrim(std::basic_string<CharT, Traits, Allocator>& str)
  652. {
  653. return asio2::trim_left(str);
  654. }
  655. /**
  656. * @brief trim left space character of the string: space \t \r \n and so on
  657. */
  658. template<
  659. class CharT,
  660. class Traits = std::char_traits<CharT>,
  661. class Allocator = std::allocator<CharT>
  662. >
  663. inline std::basic_string<CharT, Traits, Allocator> ltrim(std::basic_string<CharT, Traits, Allocator>&& str)
  664. {
  665. return asio2::trim_left(std::move(str));
  666. }
  667. /**
  668. * @brief trim right space character of the string: space \t \r \n and so on
  669. */
  670. template<
  671. class CharT,
  672. class Traits = std::char_traits<CharT>,
  673. class Allocator = std::allocator<CharT>
  674. >
  675. inline std::basic_string<CharT, Traits, Allocator>& trim_right(std::basic_string<CharT, Traits, Allocator>& str)
  676. {
  677. str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch) { return !std::isspace(ch); }).base(), str.end());
  678. return str;
  679. }
  680. /**
  681. * @brief trim right space character of the string: space \t \r \n and so on
  682. */
  683. template<
  684. class CharT,
  685. class Traits = std::char_traits<CharT>,
  686. class Allocator = std::allocator<CharT>
  687. >
  688. inline std::basic_string<CharT, Traits, Allocator> trim_right(std::basic_string<CharT, Traits, Allocator>&& str)
  689. {
  690. str.erase(std::find_if(str.rbegin(), str.rend(), [](int ch) { return !std::isspace(ch); }).base(), str.end());
  691. return std::move(str);
  692. }
  693. /**
  694. * @brief trim right space character of the string: space \t \r \n and so on
  695. */
  696. template<
  697. class CharT,
  698. class Traits = std::char_traits<CharT>,
  699. class Allocator = std::allocator<CharT>
  700. >
  701. inline std::basic_string<CharT, Traits, Allocator>& rtrim(std::basic_string<CharT, Traits, Allocator>& str)
  702. {
  703. return asio2::trim_right(str);
  704. }
  705. /**
  706. * @brief trim right space character of the string: space \t \r \n and so on
  707. */
  708. template<
  709. class CharT,
  710. class Traits = std::char_traits<CharT>,
  711. class Allocator = std::allocator<CharT>
  712. >
  713. inline std::basic_string<CharT, Traits, Allocator> rtrim(std::basic_string<CharT, Traits, Allocator>&& str)
  714. {
  715. return asio2::trim_right(std::move(str));
  716. }
  717. /**
  718. * @brief trim left and right space character of the string: space \t \r \n and so on
  719. */
  720. template<
  721. class CharT,
  722. class Traits = std::char_traits<CharT>,
  723. class Allocator = std::allocator<CharT>
  724. >
  725. inline std::basic_string<CharT, Traits, Allocator>& trim_both(std::basic_string<CharT, Traits, Allocator>& str)
  726. {
  727. return trim_right(trim_left(str));
  728. }
  729. /**
  730. * @brief trim left and right space character of the string: space \t \r \n and so on
  731. */
  732. template<
  733. class CharT,
  734. class Traits = std::char_traits<CharT>,
  735. class Allocator = std::allocator<CharT>
  736. >
  737. inline std::basic_string<CharT, Traits, Allocator> trim_both(std::basic_string<CharT, Traits, Allocator>&& str)
  738. {
  739. return trim_right(trim_left(std::move(str)));
  740. }
  741. /**
  742. * @brief trim left and right space character of the string: space \t \r \n and so on
  743. */
  744. template<
  745. class CharT,
  746. class Traits = std::char_traits<CharT>,
  747. class Allocator = std::allocator<CharT>
  748. >
  749. inline std::basic_string<CharT, Traits, Allocator>& trim(std::basic_string<CharT, Traits, Allocator>& str)
  750. {
  751. return asio2::trim_both(str);
  752. }
  753. /**
  754. * @brief trim left and right space character of the string: space \t \r \n and so on
  755. */
  756. template<
  757. class CharT,
  758. class Traits = std::char_traits<CharT>,
  759. class Allocator = std::allocator<CharT>
  760. >
  761. inline std::basic_string<CharT, Traits, Allocator> trim(std::basic_string<CharT, Traits, Allocator>&& str)
  762. {
  763. return asio2::trim_both(std::move(str));
  764. }
  765. /**
  766. * @brief Trims white spaces from the left side of string.
  767. * @param str - input string to remove white spaces from.
  768. * @return Copy of input str with trimmed white spaces.
  769. */
  770. template<
  771. class CharT,
  772. class Traits = std::char_traits<CharT>,
  773. class Allocator = std::allocator<CharT>
  774. >
  775. inline std::basic_string<CharT, Traits, Allocator> trim_left_copy(std::basic_string<CharT, Traits, Allocator> str)
  776. {
  777. return asio2::trim_left(std::move(str));
  778. }
  779. /**
  780. * @brief Trims white spaces from the left side of string.
  781. * @param str - input string to remove white spaces from.
  782. * @return Copy of input str with trimmed white spaces.
  783. */
  784. template<
  785. class CharT,
  786. class Traits = std::char_traits<CharT>,
  787. class Allocator = std::allocator<CharT>
  788. >
  789. inline std::basic_string<CharT, Traits, Allocator> ltrim_copy(std::basic_string<CharT, Traits, Allocator> str)
  790. {
  791. return asio2::trim_left(std::move(str));
  792. }
  793. /**
  794. * @brief Trims white spaces from the right side of string.
  795. * @param str - input string to remove white spaces from.
  796. * @return Copy of input str with trimmed white spaces.
  797. */
  798. template<
  799. class CharT,
  800. class Traits = std::char_traits<CharT>,
  801. class Allocator = std::allocator<CharT>
  802. >
  803. inline std::basic_string<CharT, Traits, Allocator> trim_right_copy(std::basic_string<CharT, Traits, Allocator> str)
  804. {
  805. return asio2::trim_right(std::move(str));
  806. }
  807. /**
  808. * @brief Trims white spaces from the right side of string.
  809. * @param str - input string to remove white spaces from.
  810. * @return Copy of input str with trimmed white spaces.
  811. */
  812. template<
  813. class CharT,
  814. class Traits = std::char_traits<CharT>,
  815. class Allocator = std::allocator<CharT>
  816. >
  817. inline std::basic_string<CharT, Traits, Allocator> rtrim_copy(std::basic_string<CharT, Traits, Allocator> str)
  818. {
  819. return asio2::trim_right(std::move(str));
  820. }
  821. /**
  822. * @brief Trims white spaces from the both sides of string.
  823. * @param str - input string to remove white spaces from.
  824. * @return Copy of input str with trimmed white spaces.
  825. */
  826. template<
  827. class CharT,
  828. class Traits = std::char_traits<CharT>,
  829. class Allocator = std::allocator<CharT>
  830. >
  831. inline std::basic_string<CharT, Traits, Allocator> trim_copy(std::basic_string<CharT, Traits, Allocator> str)
  832. {
  833. return asio2::trim(std::move(str));
  834. }
  835. /**
  836. * @brief trim left space character of the string: space \t \r \n and so on
  837. */
  838. template<
  839. class CharT,
  840. class Traits = std::char_traits<CharT>
  841. >
  842. inline std::basic_string_view<CharT, Traits>& trim_left(std::basic_string_view<CharT, Traits>& str)
  843. {
  844. if (str.empty())
  845. return str;
  846. using size_type = typename std::basic_string_view<CharT, Traits>::size_type;
  847. size_type pos = 0;
  848. for (; pos < str.size(); ++pos)
  849. {
  850. if (!std::isspace(static_cast<unsigned char>(str[pos])))
  851. break;
  852. }
  853. str.remove_prefix(pos);
  854. return str;
  855. }
  856. /**
  857. * @brief trim left space character of the string: space \t \r \n and so on
  858. */
  859. template<
  860. class CharT,
  861. class Traits = std::char_traits<CharT>
  862. >
  863. inline std::basic_string_view<CharT, Traits> trim_left(std::basic_string_view<CharT, Traits>&& str)
  864. {
  865. if (str.empty())
  866. return std::move(str);
  867. using size_type = typename std::basic_string_view<CharT, Traits>::size_type;
  868. size_type pos = 0;
  869. for (; pos < str.size(); ++pos)
  870. {
  871. if (!std::isspace(static_cast<unsigned char>(str[pos])))
  872. break;
  873. }
  874. str.remove_prefix(pos);
  875. return std::move(str);
  876. }
  877. /**
  878. * @brief trim left space character of the string: space \t \r \n and so on
  879. */
  880. template<
  881. class CharT,
  882. class Traits = std::char_traits<CharT>
  883. >
  884. inline std::basic_string_view<CharT, Traits>& ltrim(std::basic_string_view<CharT, Traits>& str)
  885. {
  886. return asio2::trim_left(str);
  887. }
  888. /**
  889. * @brief trim left space character of the string: space \t \r \n and so on
  890. */
  891. template<
  892. class CharT,
  893. class Traits = std::char_traits<CharT>
  894. >
  895. inline std::basic_string_view<CharT, Traits> ltrim(std::basic_string_view<CharT, Traits>&& str)
  896. {
  897. return asio2::trim_left(std::move(str));
  898. }
  899. /**
  900. * @brief trim right space character of the string: space \t \r \n and so on
  901. */
  902. template<
  903. class CharT,
  904. class Traits = std::char_traits<CharT>
  905. >
  906. inline std::basic_string_view<CharT, Traits>& trim_right(std::basic_string_view<CharT, Traits>& str)
  907. {
  908. if (str.empty())
  909. return str;
  910. using size_type = typename std::basic_string_view<CharT, Traits>::size_type;
  911. size_type pos = str.size() - 1;
  912. for (; pos != size_type(-1); pos--)
  913. {
  914. if (!std::isspace(static_cast<unsigned char>(str[pos])))
  915. break;
  916. }
  917. str.remove_suffix(str.size() - pos - 1);
  918. return str;
  919. }
  920. /**
  921. * @brief trim right space character of the string: space \t \r \n and so on
  922. */
  923. template<
  924. class CharT,
  925. class Traits = std::char_traits<CharT>
  926. >
  927. inline std::basic_string_view<CharT, Traits> trim_right(std::basic_string_view<CharT, Traits>&& str)
  928. {
  929. if (str.empty())
  930. return std::move(str);
  931. using size_type = typename std::basic_string_view<CharT, Traits>::size_type;
  932. size_type pos = str.size() - 1;
  933. for (; pos != size_type(-1); pos--)
  934. {
  935. if (!std::isspace(static_cast<unsigned char>(str[pos])))
  936. break;
  937. }
  938. str.remove_suffix(str.size() - pos - 1);
  939. return std::move(str);
  940. }
  941. /**
  942. * @brief trim right space character of the string: space \t \r \n and so on
  943. */
  944. template<
  945. class CharT,
  946. class Traits = std::char_traits<CharT>
  947. >
  948. inline std::basic_string_view<CharT, Traits>& rtrim(std::basic_string_view<CharT, Traits>& str)
  949. {
  950. return asio2::trim_right(str);
  951. }
  952. /**
  953. * @brief trim right space character of the string: space \t \r \n and so on
  954. */
  955. template<
  956. class CharT,
  957. class Traits = std::char_traits<CharT>
  958. >
  959. inline std::basic_string_view<CharT, Traits> rtrim(std::basic_string_view<CharT, Traits>&& str)
  960. {
  961. return asio2::trim_right(std::move(str));
  962. }
  963. /**
  964. * @brief trim left and right space character of the string: space \t \r \n and so on
  965. */
  966. template<
  967. class CharT,
  968. class Traits = std::char_traits<CharT>
  969. >
  970. inline std::basic_string_view<CharT, Traits>& trim_both(std::basic_string_view<CharT, Traits>& str)
  971. {
  972. return asio2::trim_right(asio2::trim_left(str));
  973. }
  974. /**
  975. * @brief trim left and right space character of the string: space \t \r \n and so on
  976. */
  977. template<
  978. class CharT,
  979. class Traits = std::char_traits<CharT>
  980. >
  981. inline std::basic_string_view<CharT, Traits> trim_both(std::basic_string_view<CharT, Traits>&& str)
  982. {
  983. return asio2::trim_right(asio2::trim_left(std::move(str)));
  984. }
  985. /**
  986. * @brief trim left and right space character of the string: space \t \r \n and so on
  987. */
  988. template<
  989. class CharT,
  990. class Traits = std::char_traits<CharT>
  991. >
  992. inline std::basic_string_view<CharT, Traits>& trim(std::basic_string_view<CharT, Traits>& str)
  993. {
  994. return asio2::trim_both(str);
  995. }
  996. /**
  997. * @brief trim left and right space character of the string: space \t \r \n and so on
  998. */
  999. template<
  1000. class CharT,
  1001. class Traits = std::char_traits<CharT>
  1002. >
  1003. inline std::basic_string_view<CharT, Traits> trim(std::basic_string_view<CharT, Traits>&& str)
  1004. {
  1005. return asio2::trim_both(std::move(str));
  1006. }
  1007. /**
  1008. * @brief Replaces (in-place) the first occurrence of target with replacement.
  1009. * @param str - input string that will be modified.
  1010. * @param target - substring that will be replaced with replacement.
  1011. * @param replacement - substring that will replace target.
  1012. * @return Replacemented input string.
  1013. */
  1014. template<
  1015. class String1,
  1016. class String2,
  1017. class CharT,
  1018. class Traits = std::char_traits<CharT>,
  1019. class Allocator = std::allocator<CharT>
  1020. >
  1021. inline std::basic_string<CharT, Traits, Allocator>& replace_first(
  1022. std::basic_string<CharT, Traits, Allocator>& str,
  1023. const String1& target,
  1024. const String2& replacement)
  1025. {
  1026. auto t = asio2::to_basic_string_view(target);
  1027. auto r = asio2::to_basic_string_view(replacement);
  1028. const std::size_t start_pos = str.find(t);
  1029. if (start_pos == std::string::npos)
  1030. {
  1031. return str;
  1032. }
  1033. str.replace(start_pos, t.length(), r);
  1034. return str;
  1035. }
  1036. /**
  1037. * @brief Replaces (in-place) the first occurrence of target with replacement.
  1038. * @param str - input string that will be modified.
  1039. * @param target - substring that will be replaced with replacement.
  1040. * @param replacement - substring that will replace target.
  1041. * @return Replacemented input string.
  1042. */
  1043. template<
  1044. class String1,
  1045. class String2,
  1046. class CharT,
  1047. class Traits = std::char_traits<CharT>,
  1048. class Allocator = std::allocator<CharT>
  1049. >
  1050. inline std::basic_string<CharT, Traits, Allocator> replace_first(
  1051. std::basic_string<CharT, Traits, Allocator>&& str,
  1052. const String1& target,
  1053. const String2& replacement)
  1054. {
  1055. auto t = asio2::to_basic_string_view(target);
  1056. auto r = asio2::to_basic_string_view(replacement);
  1057. const std::size_t start_pos = str.find(t);
  1058. if (start_pos == std::string::npos)
  1059. {
  1060. return std::move(str);
  1061. }
  1062. str.replace(start_pos, t.length(), r);
  1063. return std::move(str);
  1064. }
  1065. /**
  1066. * @brief Replaces (in-place) last occurrence of target with replacement.
  1067. * @param str - input string that will be modified.
  1068. * @param target - substring that will be replaced with replacement.
  1069. * @param replacement - substring that will replace target.
  1070. * @return Replacemented input string.
  1071. */
  1072. template<
  1073. class String1,
  1074. class String2,
  1075. class CharT,
  1076. class Traits = std::char_traits<CharT>,
  1077. class Allocator = std::allocator<CharT>
  1078. >
  1079. inline std::basic_string<CharT, Traits, Allocator>& replace_last(
  1080. std::basic_string<CharT, Traits, Allocator>& str,
  1081. const String1& target,
  1082. const String2& replacement)
  1083. {
  1084. auto t = asio2::to_basic_string_view(target);
  1085. auto r = asio2::to_basic_string_view(replacement);
  1086. std::size_t start_pos = str.rfind(t);
  1087. if (start_pos == std::string::npos)
  1088. {
  1089. return str;
  1090. }
  1091. str.replace(start_pos, t.length(), r);
  1092. return str;
  1093. }
  1094. /**
  1095. * @brief Replaces (in-place) last occurrence of target with replacement.
  1096. * @param str - input string that will be modified.
  1097. * @param target - substring that will be replaced with replacement.
  1098. * @param replacement - substring that will replace target.
  1099. * @return Replacemented input string.
  1100. */
  1101. template<
  1102. class String1,
  1103. class String2,
  1104. class CharT,
  1105. class Traits = std::char_traits<CharT>,
  1106. class Allocator = std::allocator<CharT>
  1107. >
  1108. inline std::basic_string<CharT, Traits, Allocator> replace_last(
  1109. std::basic_string<CharT, Traits, Allocator>&& str,
  1110. const String1& target,
  1111. const String2& replacement)
  1112. {
  1113. auto t = asio2::to_basic_string_view(target);
  1114. auto r = asio2::to_basic_string_view(replacement);
  1115. std::size_t start_pos = str.rfind(t);
  1116. if (start_pos == std::string::npos)
  1117. {
  1118. return std::move(str);
  1119. }
  1120. str.replace(start_pos, t.length(), r);
  1121. return std::move(str);
  1122. }
  1123. /**
  1124. * @brief Replaces (in-place) all occurrences of target with replacement.
  1125. * @param str - input string that will be modified.
  1126. * @param target - substring that will be replaced with replacement.
  1127. * @param replacement - substring that will replace target.
  1128. * @return Replacemented input string.
  1129. */
  1130. template<
  1131. class String1,
  1132. class String2,
  1133. class CharT,
  1134. class Traits = std::char_traits<CharT>,
  1135. class Allocator = std::allocator<CharT>
  1136. >
  1137. inline std::basic_string<CharT, Traits, Allocator>& replace_all(
  1138. std::basic_string<CharT, Traits, Allocator>& str,
  1139. const String1& target,
  1140. const String2& replacement)
  1141. {
  1142. auto t = asio2::to_basic_string_view(target);
  1143. auto r = asio2::to_basic_string_view(replacement);
  1144. if (t.empty())
  1145. {
  1146. return str;
  1147. }
  1148. std::size_t start_pos = 0;
  1149. while ((start_pos = str.find(t, start_pos)) != std::string::npos)
  1150. {
  1151. str.replace(start_pos, t.length(), r);
  1152. start_pos += r.length();
  1153. }
  1154. return str;
  1155. }
  1156. /**
  1157. * @brief Replaces (in-place) all occurrences of target with replacement.
  1158. * @param str - input string that will be modified.
  1159. * @param target - substring that will be replaced with replacement.
  1160. * @param replacement - substring that will replace target.
  1161. * @return Replacemented input string.
  1162. */
  1163. template<
  1164. class String1,
  1165. class String2,
  1166. class CharT,
  1167. class Traits = std::char_traits<CharT>,
  1168. class Allocator = std::allocator<CharT>
  1169. >
  1170. inline std::basic_string<CharT, Traits, Allocator> replace_all(
  1171. std::basic_string<CharT, Traits, Allocator>&& str,
  1172. const String1& target,
  1173. const String2& replacement)
  1174. {
  1175. auto t = asio2::to_basic_string_view(target);
  1176. auto r = asio2::to_basic_string_view(replacement);
  1177. if (t.empty())
  1178. {
  1179. return std::move(str);
  1180. }
  1181. std::size_t start_pos = 0;
  1182. while ((start_pos = str.find(t, start_pos)) != std::string::npos)
  1183. {
  1184. str.replace(start_pos, t.length(), r);
  1185. start_pos += r.length();
  1186. }
  1187. return std::move(str);
  1188. }
  1189. /**
  1190. * @brief Replaces (in-place) all occurrences of target with replacement.
  1191. * @param str - input string that will be modified.
  1192. * @param target - substring that will be replaced with replacement.
  1193. * @param replacement - substring that will replace target.
  1194. * @return Replacemented input string.
  1195. */
  1196. template<
  1197. class String1,
  1198. class String2,
  1199. class CharT,
  1200. class Traits = std::char_traits<CharT>,
  1201. class Allocator = std::allocator<CharT>
  1202. >
  1203. inline std::basic_string<CharT, Traits, Allocator>& replace(
  1204. std::basic_string<CharT, Traits, Allocator>& str,
  1205. const String1& target,
  1206. const String2& replacement)
  1207. {
  1208. return asio2::replace_all(str, target, replacement);
  1209. }
  1210. /**
  1211. * @brief Replaces (in-place) all occurrences of target with replacement.
  1212. * @param str - input string that will be modified.
  1213. * @param target - substring that will be replaced with replacement.
  1214. * @param replacement - substring that will replace target.
  1215. * @return Replacemented input string.
  1216. */
  1217. template<
  1218. class String1,
  1219. class String2,
  1220. class CharT,
  1221. class Traits = std::char_traits<CharT>,
  1222. class Allocator = std::allocator<CharT>
  1223. >
  1224. inline std::basic_string<CharT, Traits, Allocator> replace(
  1225. std::basic_string<CharT, Traits, Allocator>&& str,
  1226. const String1& target,
  1227. const String2& replacement)
  1228. {
  1229. return asio2::replace_all(std::move(str), target, replacement);
  1230. }
  1231. /**
  1232. * @brief Checks if string str ends with specified suffix.
  1233. * @param str - input string that will be checked.
  1234. * @param suffix - searched suffix in str.
  1235. * @return True if suffix was found, false otherwise.
  1236. */
  1237. template<
  1238. class CharT,
  1239. class Traits = std::char_traits<CharT>
  1240. >
  1241. inline bool ends_with(
  1242. std::basic_string_view<CharT, Traits> str,
  1243. std::basic_string_view<CharT, Traits> suffix)
  1244. {
  1245. const auto suffix_start = str.size() - suffix.size();
  1246. const auto result = str.find(suffix, suffix_start);
  1247. return (result == suffix_start) && (result != std::string_view::npos);
  1248. }
  1249. /**
  1250. * @brief Checks if string str ends with specified suffix.
  1251. * @param str - input string that will be checked.
  1252. * @param suffix - searched suffix in str.
  1253. * @return True if suffix was found, false otherwise.
  1254. */
  1255. template<class String1, class String2>
  1256. inline bool ends_with(const String1& str1, const String2& str2)
  1257. {
  1258. return asio2::ends_with(asio2::to_basic_string_view(str1), asio2::to_basic_string_view(str2));
  1259. }
  1260. /**
  1261. * @brief Checks if string str starts with specified prefix.
  1262. * @param str - input string that will be checked.
  1263. * @param prefix - searched prefix in str.
  1264. * @return True if prefix was found, false otherwise.
  1265. */
  1266. template<
  1267. class CharT,
  1268. class Traits = std::char_traits<CharT>
  1269. >
  1270. inline bool starts_with(
  1271. std::basic_string_view<CharT, Traits> str,
  1272. std::basic_string_view<CharT, Traits> prefix)
  1273. {
  1274. return str.rfind(prefix, 0) == 0;
  1275. }
  1276. /**
  1277. * @brief Checks if string str starts with specified prefix.
  1278. * @param str - input string that will be checked.
  1279. * @param prefix - searched prefix in str.
  1280. * @return True if prefix was found, false otherwise.
  1281. */
  1282. template<class String1, class String2>
  1283. inline bool starts_with(const String1& str1, const String2& str2)
  1284. {
  1285. return asio2::starts_with(asio2::to_basic_string_view(str1), asio2::to_basic_string_view(str2));
  1286. }
  1287. /**
  1288. * @brief Splits input string str according to input string delim.
  1289. * @param str - string that will be split.
  1290. * @param delim - the delimiter.
  1291. * @return std::vector<string> that contains all splitted tokens.
  1292. */
  1293. template<
  1294. class String1,
  1295. class CharT,
  1296. class Traits = std::char_traits<CharT>,
  1297. class Allocator = std::allocator<CharT>
  1298. >
  1299. inline std::vector<std::basic_string<CharT, Traits, Allocator>> split(
  1300. const std::basic_string<CharT, Traits, Allocator>& str,
  1301. const String1& delim)
  1302. {
  1303. auto d = asio2::to_basic_string_view(delim);
  1304. std::size_t pos_start = 0, pos_end, delim_len = d.length();
  1305. std::basic_string<CharT, Traits, Allocator> token;
  1306. std::vector<std::basic_string<CharT, Traits, Allocator>> tokens;
  1307. while ((pos_end = str.find(d, pos_start)) != std::string::npos)
  1308. {
  1309. token = str.substr(pos_start, pos_end - pos_start);
  1310. pos_start = pos_end + delim_len;
  1311. tokens.emplace_back(std::move(token));
  1312. }
  1313. tokens.emplace_back(str.substr(pos_start));
  1314. return tokens;
  1315. }
  1316. /**
  1317. * @brief Splits input string str according to input string delim.
  1318. * @param str - string that will be split.
  1319. * @param delim - the delimiter.
  1320. * @return std::vector<string_view> that contains all splitted tokens.
  1321. */
  1322. template<
  1323. class String1,
  1324. class CharT,
  1325. class Traits = std::char_traits<CharT>
  1326. >
  1327. inline std::vector<std::basic_string_view<CharT, Traits>> split(
  1328. const std::basic_string_view<CharT, Traits>& str,
  1329. const String1& delim)
  1330. {
  1331. auto d = asio2::to_basic_string_view(delim);
  1332. std::size_t pos_start = 0, pos_end, delim_len = d.length();
  1333. std::basic_string_view<CharT, Traits> token;
  1334. std::vector<std::basic_string_view<CharT, Traits>> tokens;
  1335. while ((pos_end = str.find(d, pos_start)) != std::string::npos)
  1336. {
  1337. token = str.substr(pos_start, pos_end - pos_start);
  1338. pos_start = pos_end + delim_len;
  1339. tokens.emplace_back(std::move(token));
  1340. }
  1341. tokens.emplace_back(str.substr(pos_start));
  1342. return tokens;
  1343. }
  1344. /**
  1345. * @brief Splits input string str according to input string delim.
  1346. * @param str - string that will be split.
  1347. * @param delim - the delimiter.
  1348. * @return std::vector<string> that contains all splitted tokens.
  1349. */
  1350. template<class String1, class String2>
  1351. inline auto split(const String1& str, const String2& delim)
  1352. {
  1353. using CharT = typename detail::char_type<String1>::type;
  1354. auto s = asio2::to_basic_string_view(str);
  1355. auto d = asio2::to_basic_string_view(delim);
  1356. std::size_t pos_start = 0, pos_end, delim_len = d.length();
  1357. std::basic_string<CharT> token;
  1358. std::vector<std::basic_string<CharT>> tokens;
  1359. while ((pos_end = s.find(d, pos_start)) != std::string::npos)
  1360. {
  1361. token = s.substr(pos_start, pos_end - pos_start);
  1362. pos_start = pos_end + delim_len;
  1363. tokens.emplace_back(std::move(token));
  1364. }
  1365. tokens.emplace_back(s.substr(pos_start));
  1366. return tokens;
  1367. }
  1368. /**
  1369. * @brief Splits input string using regex as a delimiter.
  1370. * @param src - string that will be split.
  1371. * @param rgx_str - the set of delimiter characters.
  1372. * @return vector of resulting tokens.
  1373. */
  1374. template<class String1, class String2>
  1375. inline auto regex_split(const String1& src, const String2& rgx_str)
  1376. {
  1377. using CharT = typename detail::char_type<String1>::type;
  1378. auto s = asio2::to_basic_string(src);
  1379. auto d = asio2::to_basic_string_view(rgx_str);
  1380. using IterType = typename std::basic_string<CharT>::const_iterator;
  1381. std::vector<std::basic_string<CharT>> elems;
  1382. const std::basic_regex<CharT> rgx(d.begin(), d.end());
  1383. std::regex_token_iterator<IterType> iter(s.begin(), s.end(), rgx, -1);
  1384. std::regex_token_iterator<IterType> end;
  1385. while (iter != end)
  1386. {
  1387. elems.emplace_back(*iter);
  1388. ++iter;
  1389. }
  1390. return elems;
  1391. }
  1392. /**
  1393. * @brief Splits input string using regex as a delimiter.
  1394. * @param src - string that will be split.
  1395. * @param dest - map of matched delimiter and those being splitted.
  1396. * @param rgx_str - the set of delimiter characters.
  1397. * @return True if the parsing is successfully done.
  1398. */
  1399. template<class String1, class String2>
  1400. inline auto regex_split_map(const String1& src, const String2& rgx_str)
  1401. {
  1402. using CharT = typename detail::char_type<String1>::type;
  1403. auto d = asio2::to_basic_string_view(rgx_str);
  1404. using IterType = typename std::basic_string<CharT>::const_iterator;
  1405. std::map<std::basic_string<CharT>, std::basic_string<CharT>> dest;
  1406. std::basic_string<CharT> tstr = src + static_cast<CharT>(' ');
  1407. std::basic_regex<CharT> rgx(d.begin(), d.end());
  1408. std::regex_token_iterator<IterType> niter(tstr.begin(), tstr.end(), rgx);
  1409. std::regex_token_iterator<IterType> viter(tstr.begin(), tstr.end(), rgx, -1);
  1410. std::regex_token_iterator<IterType> end;
  1411. ++viter;
  1412. while (niter != end)
  1413. {
  1414. dest[*niter] = *viter;
  1415. ++niter;
  1416. ++viter;
  1417. }
  1418. return dest;
  1419. }
  1420. /**
  1421. * @brief Splits input string using any delimiter in the given set.
  1422. * @param str - string that will be split.
  1423. * @param delims - the set of delimiter characters.
  1424. * @return vector of resulting tokens.
  1425. */
  1426. template<
  1427. class String1,
  1428. class CharT,
  1429. class Traits = std::char_traits<CharT>,
  1430. class Allocator = std::allocator<CharT>
  1431. >
  1432. inline std::vector<std::basic_string<CharT, Traits, Allocator>> split_any(
  1433. const std::basic_string<CharT, Traits, Allocator>& str,
  1434. const String1& delims)
  1435. {
  1436. auto d = asio2::to_basic_string_view(delims);
  1437. std::basic_string<CharT, Traits, Allocator> token;
  1438. std::vector<std::basic_string<CharT, Traits, Allocator>> tokens;
  1439. std::size_t pos_start = 0;
  1440. for (std::size_t pos_end = 0; pos_end < str.length(); ++pos_end)
  1441. {
  1442. if (asio2::contains(d, str[pos_end]))
  1443. {
  1444. token = str.substr(pos_start, pos_end - pos_start);
  1445. tokens.emplace_back(std::move(token));
  1446. pos_start = pos_end + 1;
  1447. }
  1448. }
  1449. tokens.emplace_back(str.substr(pos_start));
  1450. return tokens;
  1451. }
  1452. /**
  1453. * @brief Splits input string using any delimiter in the given set.
  1454. * @param str - string that will be split.
  1455. * @param delims - the set of delimiter characters.
  1456. * @return vector of resulting tokens.
  1457. */
  1458. template<
  1459. class String1,
  1460. class CharT,
  1461. class Traits = std::char_traits<CharT>
  1462. >
  1463. inline std::vector<std::basic_string_view<CharT, Traits>> split_any(
  1464. const std::basic_string_view<CharT, Traits>& str,
  1465. const String1& delims)
  1466. {
  1467. auto d = asio2::to_basic_string_view(delims);
  1468. std::basic_string_view<CharT, Traits> token;
  1469. std::vector<std::basic_string_view<CharT, Traits>> tokens;
  1470. std::size_t pos_start = 0;
  1471. for (std::size_t pos_end = 0; pos_end < str.length(); ++pos_end)
  1472. {
  1473. if (asio2::contains(d, str[pos_end]))
  1474. {
  1475. token = str.substr(pos_start, pos_end - pos_start);
  1476. tokens.emplace_back(std::move(token));
  1477. pos_start = pos_end + 1;
  1478. }
  1479. }
  1480. tokens.emplace_back(str.substr(pos_start));
  1481. return tokens;
  1482. }
  1483. /**
  1484. * @brief Splits input string using any delimiter in the given set.
  1485. * @param str - string that will be split.
  1486. * @param delims - the set of delimiter characters.
  1487. * @return vector of resulting tokens.
  1488. */
  1489. template<class String1, class String2>
  1490. inline auto split_any(const String1& str, const String2& delims)
  1491. {
  1492. using CharT = typename detail::char_type<String1>::type;
  1493. auto s = asio2::to_basic_string_view(str);
  1494. auto d = asio2::to_basic_string_view(delims);
  1495. std::basic_string<CharT> token;
  1496. std::vector<std::basic_string<CharT>> tokens;
  1497. std::size_t pos_start = 0;
  1498. for (std::size_t pos_end = 0; pos_end < s.length(); ++pos_end)
  1499. {
  1500. if (asio2::contains(d, s[pos_end]))
  1501. {
  1502. token = s.substr(pos_start, pos_end - pos_start);
  1503. tokens.emplace_back(std::move(token));
  1504. pos_start = pos_end + 1;
  1505. }
  1506. }
  1507. tokens.emplace_back(s.substr(pos_start));
  1508. return tokens;
  1509. }
  1510. /**
  1511. * @brief Joins all elements of std::vector tokens of arbitrary datatypes
  1512. * into one string with delimiter delim.
  1513. * @tparam T - arbitrary datatype.
  1514. * @param tokens - vector of tokens.
  1515. * @param delim - the delimiter.
  1516. * @return string with joined elements of vector tokens with delimiter delim.
  1517. */
  1518. template<
  1519. class T,
  1520. class String1
  1521. >
  1522. inline auto join(const std::vector<T>& tokens, const String1& delim)
  1523. {
  1524. using CharT = typename detail::char_type<String1>::type;
  1525. std::basic_ostringstream<CharT> result;
  1526. for (auto it = tokens.begin(); it != tokens.end(); ++it)
  1527. {
  1528. if (it != tokens.begin())
  1529. {
  1530. result << delim;
  1531. }
  1532. result << *it;
  1533. }
  1534. return result.str();
  1535. }
  1536. /**
  1537. * @brief Inplace removal of all empty strings in a vector<string>
  1538. * @param tokens - vector of strings.
  1539. */
  1540. template<
  1541. class CharT,
  1542. class Traits = std::char_traits<CharT>,
  1543. class Allocator = std::allocator<CharT>
  1544. >
  1545. inline void drop_empty(std::vector<std::basic_string<CharT, Traits, Allocator>>& tokens)
  1546. {
  1547. auto last = std::remove_if(tokens.begin(), tokens.end(),
  1548. [](const std::basic_string<CharT, Traits, Allocator>& s) { return s.empty(); });
  1549. tokens.erase(last, tokens.end());
  1550. }
  1551. /**
  1552. * @brief Inplace removal of all empty strings in a vector<string>
  1553. * @param tokens - vector of strings.
  1554. * @return vector of non-empty tokens.
  1555. */
  1556. template<
  1557. class CharT,
  1558. class Traits = std::char_traits<CharT>,
  1559. class Allocator = std::allocator<CharT>
  1560. >
  1561. inline std::vector<std::basic_string<CharT, Traits, Allocator>> drop_empty_copy(
  1562. std::vector<std::basic_string<CharT, Traits, Allocator>> tokens)
  1563. {
  1564. drop_empty(tokens);
  1565. return tokens;
  1566. }
  1567. /**
  1568. * @brief Inplace removal of all duplicate strings in a vector<string> where order is not to be maintained
  1569. * Taken from: C++ Primer V5
  1570. * @param tokens - vector of strings.
  1571. * @return vector of non-duplicate tokens.
  1572. */
  1573. template<
  1574. class CharT,
  1575. class Traits = std::char_traits<CharT>,
  1576. class Allocator = std::allocator<CharT>
  1577. >
  1578. inline void drop_duplicate(std::vector<std::basic_string<CharT, Traits, Allocator>>& tokens)
  1579. {
  1580. std::sort(tokens.begin(), tokens.end());
  1581. auto end_unique = std::unique(tokens.begin(), tokens.end());
  1582. tokens.erase(end_unique, tokens.end());
  1583. }
  1584. /**
  1585. * @brief Removal of all duplicate strings in a vector<string> where order is not to be maintained
  1586. * Taken from: C++ Primer V5
  1587. * @param tokens - vector of strings.
  1588. * @return vector of non-duplicate tokens.
  1589. */
  1590. template<
  1591. class CharT,
  1592. class Traits = std::char_traits<CharT>,
  1593. class Allocator = std::allocator<CharT>
  1594. >
  1595. inline std::vector<std::basic_string<CharT, Traits, Allocator>> drop_duplicate_copy(
  1596. std::vector<std::basic_string<CharT, Traits, Allocator>> tokens)
  1597. {
  1598. std::sort(tokens.begin(), tokens.end());
  1599. auto end_unique = std::unique(tokens.begin(), tokens.end());
  1600. tokens.erase(end_unique, tokens.end());
  1601. return tokens;
  1602. }
  1603. /**
  1604. * @brief Creates new string with repeated n times substring str.
  1605. * @param str - substring that needs to be repeated.
  1606. * @param n - number of iterations.
  1607. * @return string with repeated substring str.
  1608. */
  1609. template<class String1>
  1610. inline auto repeat(const String1& str, unsigned n)
  1611. {
  1612. using CharT = typename detail::char_type<String1>::type;
  1613. std::basic_string<CharT> result;
  1614. for (unsigned i = 0; i < n; ++i)
  1615. {
  1616. result += str;
  1617. }
  1618. return result;
  1619. }
  1620. /**
  1621. * @brief Checks if input string str matches specified reular expression regex.
  1622. * @param str - string to be checked.
  1623. * @param regex - the std::regex regular expression.
  1624. * @return True if regex matches str, false otherwise.
  1625. */
  1626. template<class String1>
  1627. inline bool matches(const String1& str, const std::basic_regex<typename detail::char_type<String1>::type>& regex)
  1628. {
  1629. return std::regex_match(str, regex);
  1630. }
  1631. /**
  1632. * @brief Sort input std::vector<string> strs in ascending order.
  1633. * @param strs - std::vector<string> to be checked.
  1634. */
  1635. template<typename T>
  1636. inline void sorting_ascending(std::vector<T>& strs)
  1637. {
  1638. std::sort(strs.begin(), strs.end());
  1639. }
  1640. /**
  1641. * @brief Sorted input std::vector<string> strs in descending order.
  1642. * @param strs - std::vector<string> to be checked.
  1643. */
  1644. template<typename T>
  1645. inline void sorting_descending(std::vector<T>& strs)
  1646. {
  1647. std::sort(strs.begin(), strs.end(), std::greater<T>());
  1648. }
  1649. /**
  1650. * @brief Reverse input std::vector<string> strs.
  1651. * @param strs - std::vector<string> to be checked.
  1652. */
  1653. template<typename T>
  1654. inline void reverse_inplace(std::vector<T>& strs)
  1655. {
  1656. std::reverse(strs.begin(), strs.end());
  1657. }
  1658. /**
  1659. * @brief Reverse input std::vector<string> strs.
  1660. * @param strs - std::vector<string> to be checked.
  1661. */
  1662. template<typename T>
  1663. inline std::vector<T> reverse_copy(std::vector<T> strs)
  1664. {
  1665. std::reverse(strs.begin(), strs.end());
  1666. return strs;
  1667. }
  1668. /**
  1669. * @brief Find substring in the string src, using a case-insensitive comparison.
  1670. * @return The finded index, or std::string::npos if not found.
  1671. */
  1672. template<class String1, class String2>
  1673. inline std::size_t ifind(const String1& src, const String2& dest, std::string::size_type pos = 0) noexcept
  1674. {
  1675. auto s = asio2::to_basic_string_view(src);
  1676. auto d = asio2::to_basic_string_view(dest);
  1677. if (pos >= s.size() || d.empty())
  1678. return std::string::npos;
  1679. // Outer loop
  1680. for (auto OuterIt = std::next(s.begin(), pos); OuterIt != s.end(); ++OuterIt)
  1681. {
  1682. auto InnerIt = OuterIt;
  1683. auto SubstrIt = d.begin();
  1684. for (; InnerIt != s.end() && SubstrIt != d.end(); ++InnerIt, ++SubstrIt)
  1685. {
  1686. if (std::tolower(*InnerIt) != std::tolower(*SubstrIt))
  1687. break;
  1688. }
  1689. // Substring matching succeeded
  1690. if (SubstrIt == d.end())
  1691. return std::distance(s.begin(), OuterIt);
  1692. }
  1693. return std::string::npos;
  1694. }
  1695. }
  1696. #include <asio2/base/detail/pop_options.hpp>
  1697. #endif // !__ASIO2_STRING_HPP__