String.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // String.h
  3. //
  4. // $Id: //poco/1.4/Foundation/src/String.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: String
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/String.h"
  16. namespace Poco {
  17. #if defined(POCO_NO_TEMPLATE_ICOMPARE)
  18. int icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, std::string::const_iterator it2, std::string::const_iterator end2)
  19. {
  20. std::string::size_type sz = str.size();
  21. if (pos > sz) pos = sz;
  22. if (pos + n > sz) n = sz - pos;
  23. std::string::const_iterator it1 = str.begin() + pos;
  24. std::string::const_iterator end1 = str.begin() + pos + n;
  25. while (it1 != end1 && it2 != end2)
  26. {
  27. std::string::value_type c1 = Ascii::toLower(*it1);
  28. std::string::value_type c2 = Ascii::toLower(*it2);
  29. if (c1 < c2)
  30. return -1;
  31. else if (c1 > c2)
  32. return 1;
  33. ++it1; ++it2;
  34. }
  35. if (it1 == end1)
  36. return it2 == end2 ? 0 : -1;
  37. else
  38. return 1;
  39. }
  40. int icompare(const std::string& str1, const std::string& str2)
  41. {
  42. return icompare(str1, 0, str1.size(), str2.begin(), str2.end());
  43. }
  44. int icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2)
  45. {
  46. if (n2 > str2.size()) n2 = str2.size();
  47. return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2);
  48. }
  49. int icompare(const std::string& str1, std::string::size_type n, const std::string& str2)
  50. {
  51. if (n > str2.size()) n = str2.size();
  52. return icompare(str1, 0, n, str2.begin(), str2.begin() + n);
  53. }
  54. int icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2)
  55. {
  56. return icompare(str1, pos, n, str2.begin(), str2.end());
  57. }
  58. int icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n1, const std::string& str2, std::string::size_type pos2, std::string::size_type n2)
  59. {
  60. std::string::size_type sz2 = str2.size();
  61. if (pos2 > sz2) pos2 = sz2;
  62. if (pos2 + n2 > sz2) n2 = sz2 - pos2;
  63. return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2);
  64. }
  65. int icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n, const std::string& str2, std::string::size_type pos2)
  66. {
  67. std::string::size_type sz2 = str2.size();
  68. if (pos2 > sz2) pos2 = sz2;
  69. if (pos2 + n > sz2) n = sz2 - pos2;
  70. return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n);
  71. }
  72. int icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr)
  73. {
  74. poco_check_ptr (ptr);
  75. std::string::size_type sz = str.size();
  76. if (pos > sz) pos = sz;
  77. if (pos + n > sz) n = sz - pos;
  78. std::string::const_iterator it = str.begin() + pos;
  79. std::string::const_iterator end = str.begin() + pos + n;
  80. while (it != end && *ptr)
  81. {
  82. std::string::value_type c1 = Ascii::toLower(*it);
  83. std::string::value_type c2 = Ascii::toLower(*ptr);
  84. if (c1 < c2)
  85. return -1;
  86. else if (c1 > c2)
  87. return 1;
  88. ++it; ++ptr;
  89. }
  90. if (it == end)
  91. return *ptr == 0 ? 0 : -1;
  92. else
  93. return 1;
  94. }
  95. int icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr)
  96. {
  97. return icompare(str, pos, str.size() - pos, ptr);
  98. }
  99. int icompare(const std::string& str, const std::string::value_type* ptr)
  100. {
  101. return icompare(str, 0, str.size(), ptr);
  102. }
  103. std::string replace(const std::string& str, const std::string& from, const std::string& to, std::string::size_type start)
  104. {
  105. std::string result(str);
  106. replaceInPlace(result, from, to, start);
  107. return result;
  108. }
  109. std::string replace(const std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start)
  110. {
  111. std::string result(str);
  112. replaceInPlace(result, from, to, start);
  113. return result;
  114. }
  115. std::string replace(const std::string& str, const std::string::value_type from, const std::string::value_type to, std::string::size_type start)
  116. {
  117. std::string result(str);
  118. replaceInPlace(result, from, to, start);
  119. return result;
  120. }
  121. std::string remove(const std::string& str, const std::string::value_type ch, std::string::size_type start)
  122. {
  123. std::string result(str);
  124. replaceInPlace(result, ch, 0, start);
  125. return result;
  126. }
  127. std::string& replaceInPlace(std::string& str, const std::string& from, const std::string& to, std::string::size_type start)
  128. {
  129. poco_assert (from.size() > 0);
  130. std::string result;
  131. std::string::size_type pos = 0;
  132. result.append(str, 0, start);
  133. do
  134. {
  135. pos = str.find(from, start);
  136. if (pos != std::string::npos)
  137. {
  138. result.append(str, start, pos - start);
  139. result.append(to);
  140. start = pos + from.length();
  141. }
  142. else result.append(str, start, str.size() - start);
  143. }
  144. while (pos != std::string::npos);
  145. str.swap(result);
  146. return str;
  147. }
  148. std::string& replaceInPlace(std::string& str, const std::string::value_type* from, const std::string::value_type* to, std::string::size_type start)
  149. {
  150. poco_assert (*from);
  151. std::string result;
  152. std::string::size_type pos = 0;
  153. std::string::size_type fromLen = std::strlen(from);
  154. result.append(str, 0, start);
  155. do
  156. {
  157. pos = str.find(from, start);
  158. if (pos != std::string::npos)
  159. {
  160. result.append(str, start, pos - start);
  161. result.append(to);
  162. start = pos + fromLen;
  163. }
  164. else result.append(str, start, str.size() - start);
  165. }
  166. while (pos != std::string::npos);
  167. str.swap(result);
  168. return str;
  169. }
  170. std::string& replaceInPlace(std::string& str, const std::string::value_type from, const std::string::value_type to, std::string::size_type start)
  171. {
  172. if (from == to) return str;
  173. std::string::size_type pos = 0;
  174. do
  175. {
  176. pos = str.find(from, start);
  177. if (pos != std::string::npos)
  178. {
  179. if (to) str[pos] = to;
  180. else str.erase(pos, 1);
  181. }
  182. } while (pos != std::string::npos);
  183. return str;
  184. }
  185. std::string& removeInPlace(std::string& str, const std::string::value_type ch, std::string::size_type start)
  186. {
  187. return replaceInPlace(str, ch, 0, start);
  188. }
  189. #endif
  190. } // namespace Poco