FIFE
2008.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2009 by the FIFE team * 00003 * http://www.fifengine.de * 00004 * This file is part of FIFE. * 00005 * * 00006 * FIFE is free software; you can redistribute it and/or * 00007 * modify it under the terms of the GNU Lesser General Public * 00008 * License as published by the Free Software Foundation; either * 00009 * version 2.1 of the License, or (at your option) any later version. * 00010 * * 00011 * This library is distributed in the hope that it will be useful, * 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00014 * Lesser General Public License for more details. * 00015 * * 00016 * You should have received a copy of the GNU Lesser General Public * 00017 * License along with this library; if not, write to the * 00018 * Free Software Foundation, Inc., * 00019 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * 00020 ***************************************************************************/ 00021 00022 // Standard C++ library includes 00023 #include <iostream> 00024 00025 // 3rd party library includes 00026 00027 // FIFE includes 00028 // These includes are split up in two parts, separated by one empty line 00029 // First block: files included from the FIFE root src directory 00030 // Second block: files included from the same folder 00031 #include "utf8stringeditor.h" 00032 #include "utf8.h" 00033 00034 namespace gcn { 00035 00036 int UTF8StringEditor::nextChar(const std::string & text, int byteOffset) 00037 { 00038 std::string::const_iterator c, e; 00039 00040 c = text.begin() + byteOffset; 00041 e = text.end(); 00042 00043 utf8::next(c, e); 00044 return std::string(text.begin(), c).size(); 00045 } 00046 00047 int UTF8StringEditor::prevChar(const std::string & text, int byteOffset) 00048 { 00049 std::string::const_iterator c, b; 00050 00051 c = text.begin() + byteOffset; 00052 b = text.begin(); 00053 00054 utf8::prior(c, b); 00055 return std::string(b, c).size(); 00056 } 00057 00058 int UTF8StringEditor::eraseChar(std::string & text, int byteOffset) 00059 { 00060 std::string::iterator begin, cur; 00061 begin = text.begin() + byteOffset; 00062 cur = begin; 00063 utf8::next(cur, text.end()); 00064 00065 text = std::string(text.begin(), begin) + std::string(cur, text.end()); 00066 return byteOffset; // this shouldn't change! 00067 } 00068 00069 int UTF8StringEditor::insertChar(std::string & text, int byteOffset, int ch) 00070 { 00071 std::string newText; 00072 std::string::iterator cut; 00073 int newOffset; 00074 00075 // make a temp string from left part of the caret (+6 extra chars) 00076 newText = text.substr(0, byteOffset) + " "; 00077 // append character 00078 utf8::append(ch, newText.begin() + byteOffset); 00079 // calculate newText real length 00080 cut = newText.begin() + byteOffset; 00081 utf8::next(cut, newText.end()); 00082 // cut the string to real length 00083 newText = std::string(newText.begin(), cut); 00084 newOffset = newText.size(); 00085 // make new text 00086 text = newText + text.substr(byteOffset); 00087 00088 return newOffset; 00089 } 00090 00091 int UTF8StringEditor::countChars(const std::string & text, int byteOffset) 00092 { 00093 return utf8::distance(text.begin(), text.begin() + byteOffset); 00094 } 00095 00096 int UTF8StringEditor::getOffset(const std::string & text, int charIndex) 00097 { 00098 std::string::const_iterator cur, end; 00099 int bytes = 0, i; 00100 00101 if (charIndex < 0) return 0; 00102 00103 cur = text.begin(); 00104 end = text.end(); 00105 00106 for(i = 0; i < charIndex && cur != end; i++) { 00107 utf8::next(cur, end); 00108 } 00109 00110 return std::string(text.begin(), cur).size(); 00111 } 00112 }; 00113 00114 00115 00116 00117 00118