TDME2  1.9.200
TextTools.cpp
Go to the documentation of this file.
2 
3 #include <string>
4 
5 #include <tdme/tdme.h>
6 #include <tdme/gui/GUI.h>
11 
12 using std::string;
13 using std::to_string;
14 
16 
17 using tdme::gui::GUI;
22 
23 bool TextTools::find(GUIStyledTextNode* textNode, const string& findString, bool matchCase, bool wholeWord, bool selection, bool firstSearch, int& index) {
24  auto success = false;
25  auto findStringLowerCase = matchCase == false?StringTools::toLowerCase(findString):findString;
26  auto textMutableString = textNode->getText();
27  auto textLowerCase = matchCase == false?StringTools::toLowerCase(textNode->getText().getString()):textNode->getText().getString();
28  auto textNodeController = required_dynamic_cast<GUIStyledTextNodeController*>(textNode->getController());
29  auto inSelectionAvailable = textNodeController->getIndex() != -1 && textNodeController->getSelectionIndex() != -1;
30  auto i = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::min(textNodeController->getIndex(), textNodeController->getSelectionIndex())):0;
31  auto fi = -1;
32  auto ni = textMutableString.getUtf8BinaryIndex(index);
33  auto l = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::max(textNodeController->getIndex(), textNodeController->getSelectionIndex())):textMutableString.size();
34  while (i < l) {
35  auto p = StringTools::indexOf(textLowerCase, findStringLowerCase, i);
36  if (p != string::npos && p < l) {
37  i = p + findStringLowerCase.size();
38  if (wholeWord == true) {
39  auto textCharIdxBefore = textMutableString.getUtf8CharacterIndex(p) - 1;
40  auto textCharIdxAfter = textMutableString.getUtf8CharacterIndex(p + findStringLowerCase.size());
41  auto textCharBefore = textMutableString.getUTF8CharAt(textCharIdxBefore);
42  auto textCharAfter = textMutableString.getUTF8CharAt(textCharIdxAfter);
43  if (Character::isAlphaNumeric(textCharBefore) == true || Character::isAlphaNumeric(textCharAfter) == true) {
44  continue;
45  }
46  }
47  if (fi == -1) fi = p;
48  if (ni != -1 && (firstSearch == true?p >= ni:p > ni)) {
49  index = p;
50  textNode->setTextStyle(p, p + findStringLowerCase.size() - 1, GUIColor("#ff0000"));
51  textNode->scrollToIndex(index);
52  ni = -1;
53  firstSearch = false;
54  success = true;
55  //
56  break;
57  }
58  } else {
59  break;
60  }
61  }
62  if (ni != -1 && fi != -1) {
63  textNode->setTextStyle(fi, fi + findStringLowerCase.size() - 1, GUIColor("#ff0000"));
64  index = fi;
65  textNode->scrollToIndex(index);
66  success = true;
67  }
68  //
69  return success;
70 }
71 
72 int TextTools::count(GUIStyledTextNode* textNode, const string& findString, bool matchCase, bool wholeWord, bool selection) {
73  auto findStringLowerCase = matchCase == false?StringTools::toLowerCase(findString):findString;
74  auto textMutableString = textNode->getText();
75  auto textLowerCase = matchCase == false?StringTools::toLowerCase(textNode->getText().getString()):textNode->getText().getString();
76  auto textNodeController = required_dynamic_cast<GUIStyledTextNodeController*>(textNode->getController());
77  auto inSelectionAvailable = textNodeController->getIndex() != -1 && textNodeController->getSelectionIndex() != -1;
78  auto i = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::min(textNodeController->getIndex(), textNodeController->getSelectionIndex())):0;
79  auto l = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::max(textNodeController->getIndex(), textNodeController->getSelectionIndex())):textMutableString.size();
80  auto c = 0;
81  while (i < l) {
82  auto p = StringTools::indexOf(textLowerCase, findStringLowerCase, i);
83  if (p != string::npos && p < l) {
84  i = p + findStringLowerCase.size();
85  if (wholeWord == true) {
86  auto textCharIdxBefore = textMutableString.getUtf8CharacterIndex(p) - 1;
87  auto textCharIdxAfter = textMutableString.getUtf8CharacterIndex(p + findStringLowerCase.size());
88  auto textCharBefore = textMutableString.getUTF8CharAt(textCharIdxBefore);
89  auto textCharAfter = textMutableString.getUTF8CharAt(textCharIdxAfter);
90  if (Character::isAlphaNumeric(textCharBefore) == true || Character::isAlphaNumeric(textCharAfter) == true) {
91  continue;
92  }
93  }
94  textNode->setTextStyle(p, p + findStringLowerCase.size() - 1, GUIColor("#ff0000"));
95  c++;
96  } else {
97  break;
98  }
99  }
100  //
101  return c;
102 }
103 
104 bool TextTools::replace(GUIStyledTextNode* textNode, const string& findString, const string& replaceString, bool matchCase, bool wholeWord, bool selection, int& index) {
105  auto success = false;
106  auto findStringLowerCase = matchCase == false?StringTools::toLowerCase(findString):findString;
107  auto findStringLength = StringTools::getUtf8Length(findString);
108  auto textMutableString = textNode->getText();
109  auto textLowerCase = matchCase == false?StringTools::toLowerCase(textMutableString.getString()):textMutableString.getString();
110  auto textNodeController = required_dynamic_cast<GUIStyledTextNodeController*>(textNode->getController());
111  auto inSelectionAvailable = textNodeController->getIndex() != -1 && textNodeController->getSelectionIndex() != -1;
112  auto i = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::min(textNodeController->getIndex(), textNodeController->getSelectionIndex())):0;
113  auto fi = -1;
114  auto ni = textMutableString.getUtf8BinaryIndex(index);
115  auto l = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::max(textNodeController->getIndex(), textNodeController->getSelectionIndex())):textMutableString.size();
116  while (i < l) {
117  auto p = StringTools::indexOf(textLowerCase, findStringLowerCase, i);
118  if (p != string::npos && p < l) {
119  i = p + findStringLowerCase.size();
120  if (wholeWord == true) {
121  auto textCharIdxBefore = textMutableString.getUtf8CharacterIndex(p) - 1;
122  auto textCharIdxAfter = textMutableString.getUtf8CharacterIndex(p + findStringLowerCase.size());
123  auto textCharBefore = textMutableString.getUTF8CharAt(textCharIdxBefore);
124  auto textCharAfter = textMutableString.getUTF8CharAt(textCharIdxAfter);
125  if (Character::isAlphaNumeric(textCharBefore) == true || Character::isAlphaNumeric(textCharAfter) == true) {
126  continue;
127  }
128  }
129  if (fi == -1) fi = p;
130  if (ni != -1 && p >= ni) {
131  //
132  auto replaceIndex = textMutableString.getUtf8CharacterIndex(p);
133  textNodeController->replace(replaceString, replaceIndex, findStringLength);
134  //
135  index = replaceIndex + findStringLength;
136  textNode->scrollToIndex(index);
137  ni = -1;
138  success = true;
139  //
140  break;
141  }
142  } else {
143  break;
144  }
145  }
146  if (ni != -1 && fi != -1) {
147  //
148  auto replaceIndex = textMutableString.getUtf8CharacterIndex(fi);
149  textNodeController->replace(replaceString, replaceIndex, findStringLength);
150  //
151  index = replaceIndex + findStringLength;
152  textNode->scrollToIndex(index);
153  success = true;
154  }
155  //
156  return success;
157 }
158 
159 bool TextTools::replaceAll(GUIStyledTextNode* textNode, const string& findString, const string& replaceString, bool matchCase, bool wholeWord, bool selection) {
160  auto success = false;
161  auto findStringLowerCase = matchCase == false?StringTools::toLowerCase(findString):findString;
162  auto findStringLength = StringTools::getUtf8Length(findString);
163  auto textMutableString = textNode->getText();
164  auto textLowerCase = matchCase == false?StringTools::toLowerCase(textNode->getText().getString()):textNode->getText().getString();
165  auto textNodeController = required_dynamic_cast<GUIStyledTextNodeController*>(textNode->getController());
166  auto inSelectionAvailable = textNodeController->getIndex() != -1 && textNodeController->getSelectionIndex() != -1;
167  auto i = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::min(textNodeController->getIndex(), textNodeController->getSelectionIndex())):0;
168  auto l = selection == true && inSelectionAvailable == true?textMutableString.getUtf8BinaryIndex(Math::max(textNodeController->getIndex(), textNodeController->getSelectionIndex())):textMutableString.size();
169  while (i < l) {
170  auto p = StringTools::indexOf(textLowerCase, findStringLowerCase, i);
171  if (p != string::npos && p < l) {
172  i = p + replaceString.size();
173  if (wholeWord == true) {
174  auto textCharIdxBefore = textMutableString.getUtf8CharacterIndex(p) - 1;
175  auto textCharIdxAfter = textMutableString.getUtf8CharacterIndex(p + findStringLowerCase.size());
176  auto textCharBefore = textMutableString.getUTF8CharAt(textCharIdxBefore);
177  auto textCharAfter = textMutableString.getUTF8CharAt(textCharIdxAfter);
178  if (Character::isAlphaNumeric(textCharBefore) == true || Character::isAlphaNumeric(textCharAfter) == true) {
179  continue;
180  }
181  }
182  //
183  auto replaceIndex = textMutableString.getUtf8CharacterIndex(p);
184  textNodeController->replace(replaceString, replaceIndex, findStringLength);
185  //
186  textMutableString = textNode->getText();
187  textLowerCase = matchCase == false?StringTools::toLowerCase(textNode->getText().getString()):textNode->getText().getString();
188  success = true;
189  } else {
190  break;
191  }
192  }
193  return success;
194 }
GUI module class.
Definition: GUI.h:64
GUINodeController * getController()
Definition: GUINode.h:661
void setTextStyle(int startIdx, int endIdx, const GUIColor &color, const string &font=string(), int size=-1, const string &url=string())
Set text style.
void scrollToIndex(int index)
Set scroll to index.
const MutableString & getText() const
static int count(GUIStyledTextNode *textNode, const string &findString, bool matchCase, bool wholeWord, bool selection)
Count string.
Definition: TextTools.cpp:72
static bool replace(GUIStyledTextNode *textNode, const string &findString, const string &replaceString, bool matchCase, bool wholeWord, bool selection, int &index)
Replace string.
Definition: TextTools.cpp:104
static bool replaceAll(GUIStyledTextNode *textNode, const string &findString, const string &replaceString, bool matchCase, bool wholeWord, bool selection)
Replace all string.
Definition: TextTools.cpp:159
Character class.
Definition: Character.h:17
const string & getString() const
String tools class.
Definition: StringTools.h:22