UTF8String.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // UTF8String.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/UTF8String.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Text
  8. // Module: UTF8String
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/UTF8String.h"
  16. #include "Poco/Unicode.h"
  17. #include "Poco/TextIterator.h"
  18. #include "Poco/TextConverter.h"
  19. #include "Poco/UTF8Encoding.h"
  20. #include <algorithm>
  21. namespace Poco {
  22. namespace
  23. {
  24. static UTF8Encoding utf8;
  25. }
  26. int UTF8::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)
  27. {
  28. std::string::size_type sz = str.size();
  29. if (pos > sz) pos = sz;
  30. if (pos + n > sz) n = sz - pos;
  31. TextIterator uit1(str.begin() + pos, str.begin() + pos + n, utf8);
  32. TextIterator uend1(str.begin() + pos + n);
  33. TextIterator uit2(it2, end2, utf8);
  34. TextIterator uend2(end2);
  35. while (uit1 != uend1 && uit2 != uend2)
  36. {
  37. int c1 = Unicode::toLower(*uit1);
  38. int c2 = Unicode::toLower(*uit2);
  39. if (c1 < c2)
  40. return -1;
  41. else if (c1 > c2)
  42. return 1;
  43. ++uit1; ++uit2;
  44. }
  45. if (uit1 == uend1)
  46. return uit2 == uend2 ? 0 : -1;
  47. else
  48. return 1;
  49. }
  50. int UTF8::icompare(const std::string& str1, const std::string& str2)
  51. {
  52. return icompare(str1, 0, str1.size(), str2.begin(), str2.end());
  53. }
  54. int UTF8::icompare(const std::string& str1, std::string::size_type n1, const std::string& str2, std::string::size_type n2)
  55. {
  56. if (n2 > str2.size()) n2 = str2.size();
  57. return icompare(str1, 0, n1, str2.begin(), str2.begin() + n2);
  58. }
  59. int UTF8::icompare(const std::string& str1, std::string::size_type n, const std::string& str2)
  60. {
  61. if (n > str2.size()) n = str2.size();
  62. return icompare(str1, 0, n, str2.begin(), str2.begin() + n);
  63. }
  64. int UTF8::icompare(const std::string& str1, std::string::size_type pos, std::string::size_type n, const std::string& str2)
  65. {
  66. return icompare(str1, pos, n, str2.begin(), str2.end());
  67. }
  68. int UTF8::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)
  69. {
  70. std::string::size_type sz2 = str2.size();
  71. if (pos2 > sz2) pos2 = sz2;
  72. if (pos2 + n2 > sz2) n2 = sz2 - pos2;
  73. return icompare(str1, pos1, n1, str2.begin() + pos2, str2.begin() + pos2 + n2);
  74. }
  75. int UTF8::icompare(const std::string& str1, std::string::size_type pos1, std::string::size_type n, const std::string& str2, std::string::size_type pos2)
  76. {
  77. std::string::size_type sz2 = str2.size();
  78. if (pos2 > sz2) pos2 = sz2;
  79. if (pos2 + n > sz2) n = sz2 - pos2;
  80. return icompare(str1, pos1, n, str2.begin() + pos2, str2.begin() + pos2 + n);
  81. }
  82. int UTF8::icompare(const std::string& str, std::string::size_type pos, std::string::size_type n, const std::string::value_type* ptr)
  83. {
  84. poco_check_ptr (ptr);
  85. std::string str2(ptr); // TODO: optimize
  86. return icompare(str, pos, n, str2.begin(), str2.end());
  87. }
  88. int UTF8::icompare(const std::string& str, std::string::size_type pos, const std::string::value_type* ptr)
  89. {
  90. return icompare(str, pos, str.size() - pos, ptr);
  91. }
  92. int UTF8::icompare(const std::string& str, const std::string::value_type* ptr)
  93. {
  94. return icompare(str, 0, str.size(), ptr);
  95. }
  96. std::string UTF8::toUpper(const std::string& str)
  97. {
  98. std::string result;
  99. TextConverter converter(utf8, utf8);
  100. converter.convert(str, result, Unicode::toUpper);
  101. return result;
  102. }
  103. std::string& UTF8::toUpperInPlace(std::string& str)
  104. {
  105. std::string result;
  106. TextConverter converter(utf8, utf8);
  107. converter.convert(str, result, Unicode::toUpper);
  108. std::swap(str, result);
  109. return str;
  110. }
  111. std::string UTF8::toLower(const std::string& str)
  112. {
  113. std::string result;
  114. TextConverter converter(utf8, utf8);
  115. converter.convert(str, result, Unicode::toLower);
  116. return result;
  117. }
  118. std::string& UTF8::toLowerInPlace(std::string& str)
  119. {
  120. std::string result;
  121. TextConverter converter(utf8, utf8);
  122. converter.convert(str, result, Unicode::toLower);
  123. std::swap(str, result);
  124. return str;
  125. }
  126. void UTF8::removeBOM(std::string& str)
  127. {
  128. if (str.size() >= 3
  129. && static_cast<unsigned char>(str[0]) == 0xEF
  130. && static_cast<unsigned char>(str[1]) == 0xBB
  131. && static_cast<unsigned char>(str[2]) == 0xBF)
  132. {
  133. str.erase(0, 3);
  134. }
  135. }
  136. } // namespace Poco