FIFE  6e1afdbeda11afe9ac53e6023a4be96ef88f1dc6
utf8stringeditor.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2009 by the FIFE team *
3  * http://www.fifengine.net *
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 "util/utf8/utf8.h"
32 
33 
34 #include "utf8stringeditor.h"
35 
36 namespace fcn {
37 
38  int UTF8StringEditor::nextChar(const std::string & text, int byteOffset)
39  {
40  std::string::const_iterator c, e;
41 
42  c = text.begin() + byteOffset;
43  e = text.end();
44 
45  utf8::next(c, e);
46  return std::string(text.begin(), c).size();
47  }
48 
49  int UTF8StringEditor::prevChar(const std::string & text, int byteOffset)
50  {
51  std::string::const_iterator c, b;
52 
53  c = text.begin() + byteOffset;
54  b = text.begin();
55 
56  utf8::prior(c, b);
57  return std::string(b, c).size();
58  }
59 
60  int UTF8StringEditor::eraseChar(std::string & text, int byteOffset)
61  {
62  std::string::iterator begin, cur;
63  begin = text.begin() + byteOffset;
64  cur = begin;
65  utf8::next(cur, text.end());
66 
67  text = std::string(text.begin(), begin) + std::string(cur, text.end());
68  return byteOffset; // this shouldn't change!
69  }
70 
71  int UTF8StringEditor::insertChar(std::string & text, int byteOffset, int ch)
72  {
73  std::string newText;
74  std::string::iterator cut;
75  int newOffset;
76 
77  // make a temp string from left part of the caret (+6 extra chars)
78  newText = text.substr(0, byteOffset) + " ";
79  // append character
80  utf8::append(ch, newText.begin() + byteOffset);
81  // calculate newText real length
82  cut = newText.begin() + byteOffset;
83  utf8::next(cut, newText.end());
84  // cut the string to real length
85  newText = std::string(newText.begin(), cut);
86  newOffset = newText.size();
87  // make new text
88  text = newText + text.substr(byteOffset);
89 
90  return newOffset;
91  }
92 
93  int UTF8StringEditor::countChars(const std::string & text, int byteOffset)
94  {
95  return utf8::distance(text.begin(), text.begin() + byteOffset);
96  }
97 
98  int UTF8StringEditor::getOffset(const std::string & text, int charIndex)
99  {
100  std::string::const_iterator cur, end;
101  int bytes = 0, i;
102 
103  if (charIndex < 0) return 0;
104 
105  cur = text.begin();
106  end = text.end();
107 
108  for(i = 0; i < charIndex && cur != end; i++) {
109  utf8::next(cur, end);
110  }
111 
112  return std::string(text.begin(), cur).size();
113  }
114 };
115 
116 
117 
118 
119 
120 
static int insertChar(std::string &text, int byteOffset, int ch)
Insert a character at specified byte offset.
static int nextChar(const std::string &text, int byteOffset)
Returns byte offset of the next character.
uint32_t next(octet_iterator &it, octet_iterator end)
Definition: checked.h:137
static int eraseChar(std::string &text, int byteOffset)
Erase character at specified byte offset.
std::iterator_traits< octet_iterator >::difference_type distance(octet_iterator first, octet_iterator last)
Definition: checked.h:198
static int getOffset(const std::string &text, int charIndex)
Gets byte offset for character index.
uint32_t prior(octet_iterator &it, octet_iterator start)
Definition: checked.h:163
static int countChars(const std::string &text, int byteOffset)
Counts characters up to byteOffset.
static int prevChar(const std::string &text, int byteOffset)
Returns byte offset of the previous character.
octet_iterator append(uint32_t cp, octet_iterator result)
The library API - functions intended to be called by the users.
Definition: checked.h:73