Change Encyclo button name and macros icon
[ryzomcore.git] / nel / src / gui / string_case.cpp
blob22c80fc5fa2c67188707ea729d02913a370e3da4
1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
6 // Copyright (C) 2020 Jan BOON (Kaetemi) <jan.boon@kaetemi.be>
7 //
8 // This program is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU Affero General Public License as
10 // published by the Free Software Foundation, either version 3 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Affero General Public License for more details.
18 // You should have received a copy of the GNU Affero General Public License
19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "stdpch.h"
22 #include "nel/gui/string_case.h"
23 #include "nel/misc/utf_string_view.h"
25 #ifdef DEBUG_NEW
26 #define new DEBUG_NEW
27 #endif
29 namespace NLGUI
31 inline bool isSeparator (char c)
33 return (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');
36 inline bool isEndSentence (char c, char lastChar)
38 // Ex: One sentence. Another sentence.
39 // ^
40 // Counterexample: nevrax.com
41 // ^
42 return ((c == ' ') || (c == '\n'))
43 && ((lastChar == '.') || (lastChar == '!') || (lastChar == '?') || (lastChar == '\n'));
46 void setCase(std::string &str, TCaseMode mode)
48 const uint length = (uint)str.length();
49 uint i;
50 bool newString = true;
51 bool newSentence = true;
52 bool newWord = true;
53 switch (mode)
55 case CaseLower:
56 str = NLMISC::toLower(str);
57 break;
58 case CaseUpper:
59 str = NLMISC::toUpper(str);
60 break;
61 case CaseFirstStringLetterUp:
63 std::string res;
64 res.reserve(str.size() + (str.size() >> 2));
65 for (ptrdiff_t i = 0; i < (ptrdiff_t)str.size();)
67 char c = str[i];
68 if (!isSeparator(c))
70 if (newString)
71 NLMISC::appendToTitle(res, str, i);
72 else
73 NLMISC::appendToLower(res, str, i);
74 newString = false;
76 else
78 res += c;
79 ++i;
82 str.swap(res);
83 break;
85 case CaseFirstSentenceLetterUp:
87 std::string res;
88 res.reserve(str.size() + (str.size() >> 2));
89 char lastChar = 0;
90 for (ptrdiff_t i = 0; i < (ptrdiff_t)str.size();)
92 char c = str[i];
93 if (isEndSentence(c, lastChar))
95 newSentence = true;
96 res += c;
97 ++i;
99 else
101 if (newSentence)
102 NLMISC::appendToTitle(res, str, i);
103 else
104 NLMISC::appendToLower(res, str, i);
106 if (!isSeparator(c))
107 newSentence = false;
109 lastChar = c;
111 str.swap(res);
112 break;
114 case CaseFirstWordLetterUp:
116 std::string res;
117 res.reserve(str.size() + (str.size() >> 2));
118 char lastChar = 0;
119 for (ptrdiff_t i = 0; i < (ptrdiff_t)str.size();)
121 char c = str[i];
122 if (isSeparator(c) || isEndSentence(c, lastChar))
124 newWord = true;
125 res += c;
126 ++i;
128 else
130 if (newWord)
131 NLMISC::appendToTitle(res, str, i);
132 else
133 NLMISC::appendToLower(res, str, i);
135 newWord = false;
137 lastChar = c;
139 str.swap(res);
140 break;
142 default:
143 break;
148 /* end of file */