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