Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #include <locale>
  2. #include <codecvt>
  3. #include <string>
  4. #include <iostream>
  5.  
  6. #include <gtest/gtest.h>
  7.  
  8. template <typename CharT, typename F>
  9. int FilteredCompare(const CharT *lb, const CharT * const le,
  10. const CharT *rb, const CharT * const re,
  11. F lnot_cared, F rnot_cared) {
  12.  
  13. while (true) {
  14.  
  15. while (lb < le && lnot_cared(*lb))
  16. ++lb;
  17. if (lb >= le)
  18. break;
  19.  
  20. while (rb < re && rnot_cared(*rb))
  21. ++rb;
  22. if (rb >= re)
  23. break;
  24.  
  25. if (!std::char_traits<CharT>::eq(*lb, *rb))
  26. return std::char_traits<CharT>::lt(*lb, *rb) ? -1 : 1;
  27.  
  28. ++lb;
  29. ++rb;
  30. }
  31.  
  32. // 此时只可能是: lb >= le 或者 rb >= re
  33. if (lb >= le) {
  34. for (; rb < re; ++rb) {
  35. if (!rnot_cared(*rb))
  36. return -1;
  37. }
  38. } else { // 此时只可能是 rb >= re
  39. for (; lb < le; ++lb) {
  40. if (!lnot_cared(*lb))
  41. return 1;
  42. }
  43. }
  44. return 0;
  45. }
  46.  
  47. template <typename CharT,typename CharT2, typename F>
  48. int FilteredCaseCompare(const CharT *lb, const CharT * const le,
  49. const CharT *rb, const CharT * const re,
  50. F lnot_cared, F rnot_cared,
  51. const std::ctype<CharT2> &ctype_facet) {
  52.  
  53. while (true) {
  54.  
  55. while (lb < le && lnot_cared(*lb))
  56. ++lb;
  57. if (lb >= le)
  58. break;
  59.  
  60. while (rb < re && rnot_cared(*rb))
  61. ++rb;
  62. if (rb >= re)
  63. break;
  64.  
  65. CharT lc = ctype_facet.toupper(*lb);
  66. CharT rc = ctype_facet.toupper(*rb);
  67. if (!std::char_traits<CharT>::eq(lc, rc))
  68. return std::char_traits<CharT>::lt(lc, rc) ? -1 : 1;
  69.  
  70. ++lb;
  71. ++rb;
  72. }
  73.  
  74. // 此时只可能是: lb >= le 或者 rb >= re
  75. if (lb >= le) {
  76. for (; rb < re; ++rb) {
  77. if (!rnot_cared(*rb))
  78. return -1;
  79. }
  80. } else { // 此时只可能是 rb >= re
  81. for (; lb < le; ++lb) {
  82. if (!lnot_cared(*lb))
  83. return 1;
  84. }
  85. }
  86. return 0;
  87. }
  88.  
  89. template <typename CharT, typename F>
  90. inline int FilteredCompare(const CharT *lb, const CharT * const le,
  91. const CharT *rb, const CharT * const re,
  92. F not_cared) {
  93. return FilteredCompare(lb, le, rb, re, not_cared, not_cared);
  94. }
  95.  
  96. template <typename CharT, typename CharT2, typename F>
  97. inline int FilteredCaseCompare(const CharT *lb, const CharT * const le,
  98. const CharT *rb, const CharT * const re,
  99. F not_cared,
  100. const std::ctype<CharT2> &ctype_facet) {
  101. return FilteredCaseCompare(lb, le, rb, re, not_cared, not_cared, ctype_facet);
  102. }
  103.  
  104. namespace {
  105.  
  106. const std::locale g_en_us_utf8_loc("en_US.UTF8");
  107. const std::ctype<wchar_t> &g_unicode_ctype = std::use_facet<std::ctype<wchar_t>>(g_en_us_utf8_loc);
  108.  
  109. } // namespace
  110.  
  111. bool IsEqualedTitle(const std::u32string &left_title, const std::u32string &right_title) {
  112.  
  113. auto IsntCared = [] (char32_t ch) -> bool {
  114. return g_unicode_ctype.is(
  115. std::ctype_base::space |
  116. std::ctype_base::cntrl |
  117. std::ctype_base::punct |
  118. std::ctype_base::blank,
  119. ch);
  120. };
  121.  
  122. return FilteredCaseCompare(left_title.data(), left_title.data() + left_title.size(),
  123. right_title.data(), right_title.data() + right_title.size(),
  124. IsntCared,
  125. g_unicode_ctype) == 0;
  126. }
  127.  
  128. inline std::u32string Utf8ToUtf32(const std::string &utf8) {
  129. return std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t>{}.from_bytes(utf8);
  130. }
  131.  
  132. TEST(IsEqualedTitleTest,test) {
  133.  
  134. auto Test = [] (const std::string &left, const std::string &right, bool expected) {
  135. EXPECT_EQ(expected, IsEqualedTitle(Utf8ToUtf32(left), Utf8ToUtf32(right)));
  136. return ;
  137. };
  138.  
  139. Test("hello", "hello", true);
  140. Test("您好hello", "您好HeLlO", true);
  141. Test("您 好 h e l l o", "您 好H eL l O", true);
  142. Test("您,好 . h ::: e l ' l o", "您 好H. eL, l ,.';' O", true);
  143. Test("您,。‘好’ . h :。:: e` “”l ' l o", "您 好H. 。 eL, 。 l ,.';' 《》 O", true);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement