Guest User

Untitled

a guest
Sep 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. #include "engine.hpp"
  2.  
  3. namespace BoGo {
  4.  
  5. using namespace Glib;
  6. using namespace std;
  7.  
  8. #ifdef _
  9. #undef _
  10. #endif
  11. #define _(str) ustring (ustring ("") + str)
  12.  
  13. #ifdef __
  14. #undef __
  15. #endif
  16. #define __(ch) ustring (ustring ("") + ch).c_str ()
  17.  
  18. const ustring LowerCaseAlphabet = _("aăâbcdeêfghijklmnoôơpqrstuưvwxy");
  19. const ustring UpperCaseAlphabet = _("AĂÂBCDEÊFGHIJKLMNOÔƠPQRSTUƯVWXY");
  20. const ustring Numbers = _("0123456789");
  21.  
  22. const ustring Vowels = _("aăâeêioôơuưy");
  23. // Vowels with Accents should match Vowels and Accents, see header
  24. // file
  25. const ustring VowelsWithAccents[] = {
  26. _("àáảãạ"), _("ằắẳẵặ"), _("ầấẩẫậ"),
  27. _("èéẻẽẹ"), _("ềếểễệ"), _("ìíỉĩị"),
  28. _("òóỏõọ"), _("ồốổỗộ"), _("ờớởỡợ"),
  29. _("ùúủũụ"), _("ừứửữự"), _("ỳýỷỹỵ")
  30. };
  31.  
  32. const ustring VowelsWithoutHat = _("aàáảãạeèéẻẽẹoòóỏõọ");
  33. const ustring VowelsWithHat = _("âầấẩẫậêềếểễệôồốổỗộ");
  34.  
  35. const ustring VowelsWithoutHorn = _("aàáảãạoòóỏõọuùúủũụ");
  36. const ustring VowelsWithHorn = _("ăằắẳãạơờớởỡợưừứửữự");
  37.  
  38. // Separate
  39. ustring replace (ustring str, ustring oldPortion,
  40. ustring newPortion, _size_t startFrom = 0) {
  41. _size_t pos = str.find (oldPortion, startFrom);
  42. if (pos != ustring::npos)
  43. str.replace (pos, oldPortion.length (), newPortion);
  44. return str;
  45. }
  46.  
  47. bool isVietnameseAlphanumeric (ustring str) {
  48. return (LowerCaseAlphabet.find (str) != ustring::npos)
  49. || (UpperCaseAlphabet.find (str) != ustring::npos)
  50. || (Numbers.find (str) != ustring::npos);
  51. }
  52.  
  53. bool isVietnameseAlphanumeric (std::string str) {
  54. return isVietnameseAlphanumeric (_(str));
  55. }
  56.  
  57. bool isVietnameseAlphanumeric (const char *str) {
  58. return isVietnameseAlphanumeric (_(str));
  59. }
  60.  
  61. bool isWordBreak (ustring str) {
  62. return (LowerCaseAlphabet.find (str) == ustring::npos)
  63. && (UpperCaseAlphabet.find (str) == ustring::npos);
  64. }
  65. //
  66.  
  67. ustring removeAccent (ustring str, int pos) {
  68. for (guint iVowel = 0; iVowel < Vowels.length (); iVowel++)
  69. for (guint accent = GRAVE; accent <= DOT; accent++)
  70. if (str[pos] == VowelsWithAccents[iVowel][accent]) {
  71. str.replace (pos, 1, _(Vowels[iVowel]));
  72. break;
  73. }
  74. return str;
  75. }
  76.  
  77. ustring removeHat (ustring str, int pos) {
  78. _size_t hatPos = VowelsWithHat.find (str[pos]);
  79. if (hatPos != ustring::npos)
  80. str.replace (pos, 1, _(VowelsWithoutHat[hatPos]));
  81. return str;
  82. }
  83.  
  84. ustring removeHorn (ustring str, int pos) {
  85. _size_t hornPos = VowelsWithHorn.find (str[pos]);
  86. if (hornPos != ustring::npos)
  87. str.replace (pos, 1, _(VowelsWithoutHorn[hornPos]));
  88. return str;
  89. }
  90.  
  91. ustring toPlainVowel (ustring str, int pos) {
  92. return removeHorn (removeHat (str, pos), pos);
  93. }
  94.  
  95. ustring addAccentToChar (ustring str, Accents accent) {
  96. str = removeAccent (str);
  97. _size_t positionInVowelList = Vowels.find (str);
  98. if (positionInVowelList != ustring::npos)
  99. str = VowelsWithAccents[positionInVowelList][accent];
  100. return str;
  101. }
  102.  
  103. ustring addAccentToChar (const char *ch, Accents accent) {
  104. return addAccentToChar (_(ch), accent);
  105. }
  106.  
  107. ustring addAccentToChar (std::string str, Accents accent) {
  108. return addAccentToChar (_(str), accent);
  109. }
  110.  
  111. ustring addHat (ustring str, _size_t startFrom) {
  112. for (_size_t i = 0; i < VowelsWithoutHat.length (); i++)
  113. str = replace (str, _(VowelsWithoutHat[i]), _(VowelsWithHat[i]));
  114. return str;
  115. }
  116.  
  117. ustring addHat (const char *str, _size_t startFrom) {
  118. return addHat (_(str), startFrom);
  119. }
  120.  
  121. ustring addHat (std::string str, _size_t startFrom) {
  122. return addHat (_(str), startFrom);
  123. }
  124.  
  125. ustring addHorn (ustring str, _size_t startFrom) {
  126. for (_size_t i = 0; i < VowelsWithoutHorn.length (); i++)
  127. str = replace (str, _(VowelsWithoutHorn[i]), _(VowelsWithHorn[i]));
  128. return str;
  129. }
  130.  
  131. ustring addHorn (const char *str, _size_t startFrom) {
  132. return addHorn (_(str), startFrom);
  133. }
  134.  
  135. ustring addHorn (std::string str, _size_t startFrom) {
  136. return addHorn (_(str), startFrom);
  137. }
  138.  
  139. #undef _
  140. #undef __
  141. }
Add Comment
Please, Sign In to add comment