utf8stringeditor.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <iostream>
00024
00025
00026
00027
00028
00029
00030
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;
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
00076 newText = text.substr(0, byteOffset) + " ";
00077
00078 utf8::append(ch, newText.begin() + byteOffset);
00079
00080 cut = newText.begin() + byteOffset;
00081 utf8::next(cut, newText.end());
00082
00083 newText = std::string(newText.begin(), cut);
00084 newOffset = newText.size();
00085
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