Mercurial > fife-parpg
annotate engine/core/util/utf8/utf8stringeditor.cpp @ 526:c916d7b7de1c
Added the goldstack image as I forgot to add it before.
author | prock@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Fri, 28 May 2010 04:15:56 +0000 |
parents | 756b895e1dab |
children |
rev | line source |
---|---|
228
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
1 /*************************************************************************** |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
2 * Copyright (C) 2009 by the FIFE team * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
3 * http://www.fifengine.de * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
4 * This file is part of FIFE. * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
5 * * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
6 * FIFE is free software; you can redistribute it and/or * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
7 * modify it under the terms of the GNU Lesser General Public * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
8 * License as published by the Free Software Foundation; either * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
9 * version 2.1 of the License, or (at your option) any later version. * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
10 * * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
11 * This library is distributed in the hope that it will be useful, * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
14 * Lesser General Public License for more details. * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
15 * * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
16 * You should have received a copy of the GNU Lesser General Public * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
17 * License along with this library; if not, write to the * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
18 * Free Software Foundation, Inc., * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
20 ***************************************************************************/ |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
21 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
22 // Standard C++ library includes |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
23 #include <iostream> |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
24 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
25 // 3rd party library includes |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
26 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
27 // FIFE includes |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
28 // These includes are split up in two parts, separated by one empty line |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
29 // First block: files included from the FIFE root src directory |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
30 // Second block: files included from the same folder |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
31 #include "utf8stringeditor.h" |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
32 #include "utf8.h" |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
33 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
34 namespace gcn { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
35 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
36 int UTF8StringEditor::nextChar(const std::string & text, int byteOffset) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
37 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
38 std::string::const_iterator c, e; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
39 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
40 c = text.begin() + byteOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
41 e = text.end(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
42 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
43 utf8::next(c, e); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
44 return std::string(text.begin(), c).size(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
45 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
46 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
47 int UTF8StringEditor::prevChar(const std::string & text, int byteOffset) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
48 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
49 std::string::const_iterator c, b; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
50 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
51 c = text.begin() + byteOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
52 b = text.begin(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
53 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
54 utf8::prior(c, b); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
55 return std::string(b, c).size(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
56 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
57 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
58 int UTF8StringEditor::eraseChar(std::string & text, int byteOffset) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
59 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
60 std::string::iterator begin, cur; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
61 begin = text.begin() + byteOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
62 cur = begin; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
63 utf8::next(cur, text.end()); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
64 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
65 text = std::string(text.begin(), begin) + std::string(cur, text.end()); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
66 return byteOffset; // this shouldn't change! |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
67 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
68 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
69 int UTF8StringEditor::insertChar(std::string & text, int byteOffset, int ch) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
70 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
71 std::string newText; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
72 std::string::iterator cut; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
73 int newOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
74 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
75 // make a temp string from left part of the caret (+6 extra chars) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
76 newText = text.substr(0, byteOffset) + " "; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
77 // append character |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
78 utf8::append(ch, newText.begin() + byteOffset); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
79 // calculate newText real length |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
80 cut = newText.begin() + byteOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
81 utf8::next(cut, newText.end()); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
82 // cut the string to real length |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
83 newText = std::string(newText.begin(), cut); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
84 newOffset = newText.size(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
85 // make new text |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
86 text = newText + text.substr(byteOffset); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
87 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
88 return newOffset; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
89 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
90 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
91 int UTF8StringEditor::countChars(const std::string & text, int byteOffset) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
92 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
93 return utf8::distance(text.begin(), text.begin() + byteOffset); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
94 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
95 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
96 int UTF8StringEditor::getOffset(const std::string & text, int charIndex) |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
97 { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
98 std::string::const_iterator cur, end; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
99 int bytes = 0, i; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
100 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
101 if (charIndex < 0) return 0; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
102 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
103 cur = text.begin(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
104 end = text.end(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
105 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
106 for(i = 0; i < charIndex && cur != end; i++) { |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
107 utf8::next(cur, end); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
108 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
109 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
110 return std::string(text.begin(), cur).size(); |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
111 } |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
112 }; |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
113 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
114 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
115 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
116 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
117 |
756b895e1dab
Merged unicode-support back into trunk.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff
changeset
|
118 |