Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "engine.hpp"
- namespace BoGo {
- using namespace Glib;
- using namespace std;
- #ifdef _
- #undef _
- #endif
- #define _(str) ustring (ustring ("") + str)
- #ifdef __
- #undef __
- #endif
- #define __(ch) ustring (ustring ("") + ch).c_str ()
- const ustring LowerCaseAlphabet = _("aăâbcdeêfghijklmnoôơpqrstuưvwxy");
- const ustring UpperCaseAlphabet = _("AĂÂBCDEÊFGHIJKLMNOÔƠPQRSTUƯVWXY");
- const ustring Numbers = _("0123456789");
- const ustring Vowels = _("aăâeêioôơuưy");
- // Vowels with Accents should match Vowels and Accents, see header
- // file
- const ustring VowelsWithAccents[] = {
- _("àáảãạ"), _("ằắẳẵặ"), _("ầấẩẫậ"),
- _("èéẻẽẹ"), _("ềếểễệ"), _("ìíỉĩị"),
- _("òóỏõọ"), _("ồốổỗộ"), _("ờớởỡợ"),
- _("ùúủũụ"), _("ừứửữự"), _("ỳýỷỹỵ")
- };
- const ustring VowelsWithoutHat = _("aàáảãạeèéẻẽẹoòóỏõọ");
- const ustring VowelsWithHat = _("âầấẩẫậêềếểễệôồốổỗộ");
- const ustring VowelsWithoutHorn = _("aàáảãạoòóỏõọuùúủũụ");
- const ustring VowelsWithHorn = _("ăằắẳãạơờớởỡợưừứửữự");
- // Separate
- ustring replace (ustring str, ustring oldPortion,
- ustring newPortion, _size_t startFrom = 0) {
- _size_t pos = str.find (oldPortion, startFrom);
- if (pos != ustring::npos)
- str.replace (pos, oldPortion.length (), newPortion);
- return str;
- }
- bool isVietnameseAlphanumeric (ustring str) {
- return (LowerCaseAlphabet.find (str) != ustring::npos)
- || (UpperCaseAlphabet.find (str) != ustring::npos)
- || (Numbers.find (str) != ustring::npos);
- }
- bool isVietnameseAlphanumeric (std::string str) {
- return isVietnameseAlphanumeric (_(str));
- }
- bool isVietnameseAlphanumeric (const char *str) {
- return isVietnameseAlphanumeric (_(str));
- }
- bool isWordBreak (ustring str) {
- return (LowerCaseAlphabet.find (str) == ustring::npos)
- && (UpperCaseAlphabet.find (str) == ustring::npos);
- }
- //
- ustring removeAccent (ustring str, int pos) {
- for (guint iVowel = 0; iVowel < Vowels.length (); iVowel++)
- for (guint accent = GRAVE; accent <= DOT; accent++)
- if (str[pos] == VowelsWithAccents[iVowel][accent]) {
- str.replace (pos, 1, _(Vowels[iVowel]));
- break;
- }
- return str;
- }
- ustring removeHat (ustring str, int pos) {
- _size_t hatPos = VowelsWithHat.find (str[pos]);
- if (hatPos != ustring::npos)
- str.replace (pos, 1, _(VowelsWithoutHat[hatPos]));
- return str;
- }
- ustring removeHorn (ustring str, int pos) {
- _size_t hornPos = VowelsWithHorn.find (str[pos]);
- if (hornPos != ustring::npos)
- str.replace (pos, 1, _(VowelsWithoutHorn[hornPos]));
- return str;
- }
- ustring toPlainVowel (ustring str, int pos) {
- return removeHorn (removeHat (str, pos), pos);
- }
- ustring addAccentToChar (ustring str, Accents accent) {
- str = removeAccent (str);
- _size_t positionInVowelList = Vowels.find (str);
- if (positionInVowelList != ustring::npos)
- str = VowelsWithAccents[positionInVowelList][accent];
- return str;
- }
- ustring addAccentToChar (const char *ch, Accents accent) {
- return addAccentToChar (_(ch), accent);
- }
- ustring addAccentToChar (std::string str, Accents accent) {
- return addAccentToChar (_(str), accent);
- }
- ustring addHat (ustring str, _size_t startFrom) {
- for (_size_t i = 0; i < VowelsWithoutHat.length (); i++)
- str = replace (str, _(VowelsWithoutHat[i]), _(VowelsWithHat[i]));
- return str;
- }
- ustring addHat (const char *str, _size_t startFrom) {
- return addHat (_(str), startFrom);
- }
- ustring addHat (std::string str, _size_t startFrom) {
- return addHat (_(str), startFrom);
- }
- ustring addHorn (ustring str, _size_t startFrom) {
- for (_size_t i = 0; i < VowelsWithoutHorn.length (); i++)
- str = replace (str, _(VowelsWithoutHorn[i]), _(VowelsWithHorn[i]));
- return str;
- }
- ustring addHorn (const char *str, _size_t startFrom) {
- return addHorn (_(str), startFrom);
- }
- ustring addHorn (std::string str, _size_t startFrom) {
- return addHorn (_(str), startFrom);
- }
- #undef _
- #undef __
- }
Add Comment
Please, Sign In to add comment