string_utils.h.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <chapter xml:id="string_utils.h">
  2. <title><tt>__vic/string_utils.h</tt></title>
  3. <p>Miscellaneous strings-related utilities.</p>
  4. <chapter xml:id="trim">
  5. <title><tt>trim</tt> functions</title>
  6. <code-block lang="C++"><![CDATA[
  7. char *trim(char *str);
  8. char *trim_front(char *str);
  9. char *trim_back(char *str);
  10. char *trim(char *str, char ch);
  11. char *trim_front(char *str, char ch);
  12. char *trim_back(char *str, char ch);
  13. char *trim(char *str, const char *set);
  14. char *trim_front(char *str, const char *set);
  15. char *trim_back(char *str, const char *set);
  16. std::string &trim(std::string &str);
  17. std::string &trim_front(std::string &str);
  18. std::string &trim_back(std::string &str);
  19. std::string &trim(std::string &str, char ch);
  20. std::string &trim_front(std::string &str, char ch);
  21. std::string &trim_back(std::string &str, char ch);
  22. std::string &trim(std::string &str, const char *set);
  23. std::string &trim_front(std::string &str, const char *set);
  24. std::string &trim_back(std::string &str, const char *set);
  25. #if __cpp_lib_string_view // C++17
  26. std::string_view trimmed(std::string_view str);
  27. std::string_view trimmed_front(std::string_view str);
  28. std::string_view trimmed_back(std::string_view str);
  29. std::string_view trimmed(std::string_view str, char ch);
  30. std::string_view trimmed_front(std::string_view str, char ch);
  31. std::string_view trimmed_back(std::string_view str, char ch);
  32. std::string_view trimmed(std::string_view str, const char *set);
  33. std::string_view trimmed_front(std::string_view str, const char *set);
  34. std::string_view trimmed_back(std::string_view str, const char *set);
  35. #else // until C++17
  36. std::string trimmed(const std::string &str);
  37. std::string trimmed_left(const std::string &str);
  38. std::string trimmed_right(const std::string &str);
  39. std::string trimmed(const std::string &str, char ch);
  40. std::string trimmed_left(const std::string &str, char ch);
  41. std::string trimmed_right(const std::string &str, char ch);
  42. std::string trimmed(const std::string &str, const char *set);
  43. std::string trimmed_left(const std::string &str, const char *set);
  44. std::string trimmed_right(const std::string &str, const char *set);
  45. #endif
  46. ]]></code-block>
  47. <p>The set of functions stripping unwanted characters from the string edges.
  48. Characters to strip can be specified. One can specify single character
  49. <tt>ch</tt> as well as the set of characters <tt>set</tt>. If no characters
  50. is specified, all ASCII-whitespaces are implied. Following naming rules for
  51. the functions are used:</p>
  52. <list style="bulleted">
  53. <item><tt>trim</tt> - strips from both edges,</item>
  54. <item><tt>trim_front</tt> - strips from the beginning,</item>
  55. <item><tt>trim_back</tt> - strips from the end.</item>
  56. </list>
  57. <p>Functions <tt>trim</tt> modify the string in-situ and return the pointer or
  58. reference to it. If the original value should be preserved, <tt>trimmed</tt>
  59. functions should be used.</p>
  60. <list style="bulleted">
  61. <item><tt>trimmed</tt> - return stripped copy of the string, argument
  62. itself is not modified.</item>
  63. </list>
  64. <p>The implementation is optimized for common case when the string does not have
  65. anything to trim. In such cases no modifications of the argument are performed,
  66. the function returns immediately after the checks are completed, and the call
  67. is maximally cheap.</p>
  68. <p>All <tt>nullptr</tt> values are treated as an empty string.</p>
  69. <section><title>Example</title>
  70. <code-block lang="C++">
  71. char st1[] = "\t value \n";
  72. // CHOICE:
  73. __vic::trim(st1); // result: "value"
  74. __vic::trim_front(st1); // result: "value \n"
  75. __vic::trim_back(st1); // result: "\t value"
  76. std::string st2("...value123");
  77. // CHOICE:
  78. // trim dot chars
  79. __vic::trim_front(st1, '.'); // result: "value123"
  80. // trim all digits
  81. __vic::trim_back(st1, "123456789"); // result: "...value"
  82. </code-block>
  83. </section>
  84. </chapter>
  85. <chapter xml:id="sift">
  86. <title><tt>sift()</tt></title>
  87. <code-block lang="C++"><![CDATA[
  88. char *sift(char *str, const char *trash_chars);
  89. std::string &sift(std::string &str, const char *trash_chars);
  90. ]]></code-block>
  91. <p>Removes all characters from the set from the string. All <tt>nullptr</tt>
  92. values are treated as an empty string.</p>
  93. <section><title>Example</title>
  94. <code-block lang="C++">
  95. char st[] = "..ab.c..d.e.";
  96. __vic::sift(st, ".");
  97. assert(std::strcmp(st, "abcde") == 0);
  98. </code-block>
  99. </section>
  100. </chapter>
  101. <chapter xml:id="sift_if">
  102. <title><tt>sift_if()</tt></title>
  103. <code-block lang="C++"><![CDATA[
  104. template<class Pred>
  105. char *sift(char *str, Pred pred);
  106. template<class Pred>
  107. std::string &sift(std::string &str, Pred pred);
  108. ]]></code-block>
  109. <p>Removes all characters satify the predicate <tt>pred</tt>. All
  110. <tt>nullptr</tt> values are treated as an empty string.</p>
  111. </chapter>
  112. <chapter xml:id="pad_front">
  113. <title><tt>pad_front()</tt></title>
  114. <code-block lang="C++"><![CDATA[
  115. std::string &pad_front(std::string &str, size_t size, char pad_ch = ' ');
  116. char *pad_front(char *str, size_t size, char pad_ch = ' ');
  117. ]]></code-block>
  118. <p>Complements the string up to length <tt>size</tt> by adding the required
  119. number of <tt>pad_ch</tt> chars to the beginning. Nothing happens if
  120. <tt>str</tt> length is equal or greather than <tt>size</tt> or the pointer is
  121. null. Returns <tt>str</tt>.</p>
  122. </chapter>
  123. <chapter xml:id="pad_back">
  124. <title><tt>pad_back()</tt></title>
  125. <code-block lang="C++"><![CDATA[
  126. std::string &pad_back(std::string &str, size_t size, char pad_ch = ' ');
  127. char *pad_back(char *str, size_t size, char pad_ch = ' ');
  128. ]]></code-block>
  129. <p>Complements the string up to length <tt>size</tt> by adding the required
  130. number of <tt>pad_ch</tt> chars to the end. Nothing happens if
  131. <tt>str</tt> length is equal or greather than <tt>size</tt> or the pointer is
  132. null. Returns
  133. <tt>str</tt>.</p>
  134. </chapter>
  135. <chapter xml:id="starts_with">
  136. <title><tt>starts_with()</tt></title>
  137. <code-block lang="C++"><![CDATA[
  138. bool starts_with(const char *s, char pref);
  139. bool starts_with(const char *s, const char *pref);
  140. bool starts_with(const char *s, const char *pref, size_t pref_len);
  141. #if __cpp_lib_string_view // C++17
  142. bool starts_with(std::string_view s, char pref);
  143. bool starts_with(std::string_view s, std::string_view pref);
  144. bool starts_with(std::string_view s, const char *pref);
  145. #else // until C++17
  146. bool starts_with(const std::string &s, char pref);
  147. bool starts_with(const std::string &s, const char *pref);
  148. bool starts_with(const std::string &s, const std::string &pref);
  149. bool starts_with(const std::string &s, const char *pref, size_t pref_len);
  150. #endif
  151. bool starts_with(const char *s, size_t s_len, char pref);
  152. bool starts_with(const char *s, size_t s_len, const char *pref);
  153. bool starts_with(const char *s, size_t s_len, const char *pref, size_t pref_len);
  154. ]]></code-block>
  155. <p>Returns <tt>true</tt> if string <tt>s</tt> starts with the specified
  156. prefix.</p>
  157. </chapter>
  158. <chapter xml:id="ends_with">
  159. <title><tt>ends_with()</tt></title>
  160. <code-block lang="C++"><![CDATA[
  161. #if __cpp_lib_string_view // C++17
  162. bool ends_with(std::string_view s, char suff);
  163. bool ends_with(std::string_view s, std::string_view suff);
  164. #else // until C++17
  165. bool ends_with(const char *s, char suff);
  166. bool ends_with(const char *s, const char *suff);
  167. bool ends_with(const char *s, const char *suff, size_t suff_len);
  168. bool ends_with(const char *s, size_t s_len, const char *suff);
  169. bool ends_with(const std::string &s, char suff);
  170. bool ends_with(const std::string &s, const char *suff);
  171. bool ends_with(const std::string &s, const std::string &suff);
  172. bool ends_with(const std::string &s, const char *suff, size_t suff_len);
  173. #endif
  174. bool ends_with(const char *s, size_t s_len, char suff);
  175. bool ends_with(const char *s, size_t s_len, const char *suff, size_t suff_len);
  176. ]]></code-block>
  177. <p>Returns <tt>true</tt> if string <tt>s</tt> ends with the specified
  178. suffix.</p>
  179. </chapter>
  180. </chapter>