PolyString.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyString.h"
  20. using namespace Polycode;
  21. String::String() {
  22. contents = L"";
  23. s_contents = "";
  24. }
  25. String::String(const wchar_t *str) {
  26. contents = wstring(str);
  27. }
  28. String::String(const char *str) {
  29. string sstr = string(str);
  30. utf8toWStr(contents, sstr);
  31. // contents.assign(sstr.begin(), sstr.end());
  32. }
  33. String::String(const wchar_t *str, size_t n) {
  34. contents = wstring(str, n);
  35. }
  36. String::String(const string& str) {
  37. contents.assign(str.begin(), str.end());
  38. }
  39. String::String(const wstring& str) {
  40. contents = str;
  41. }
  42. String::~String() {
  43. }
  44. size_t String::getDataSizeWithEncoding(int encoding) const {
  45. switch(encoding) {
  46. case ENCODING_UTF8: {
  47. string dest;
  48. wstrToUtf8(dest, contents);
  49. return dest.size();
  50. }
  51. default:
  52. return NULL;
  53. }
  54. }
  55. const char *String::getDataWithEncoding(int encoding) const {
  56. switch(encoding) {
  57. case ENCODING_UTF8: {
  58. string dest;
  59. wstrToUtf8(dest, contents);
  60. return dest.data();
  61. }
  62. break;
  63. default:
  64. return NULL;
  65. }
  66. }
  67. void String::setDataWithEncoding(char *data, int encoding) {
  68. switch(encoding) {
  69. case ENCODING_UTF8: {
  70. string str = string(data);
  71. utf8toWStr(contents, str);
  72. }
  73. default:
  74. break;
  75. }
  76. }
  77. vector<String> String::split(const String &delim) const {
  78. vector<String> tokens;
  79. bool trimEmpty = false;
  80. std::wstring::size_type pos, lastPos = 0;
  81. while(true)
  82. {
  83. pos = contents.find_first_of(delim.contents, lastPos);
  84. if(pos == std::wstring::npos)
  85. {
  86. pos = contents.length();
  87. if(pos != lastPos || !trimEmpty)
  88. tokens.push_back(vector<String>::value_type(contents.data()+lastPos, (wstring::size_type)pos-lastPos ));
  89. break;
  90. }
  91. else
  92. {
  93. if(pos != lastPos || !trimEmpty)
  94. tokens.push_back(vector<String>::value_type(contents.data()+lastPos, (wstring::size_type)pos-lastPos ));
  95. }
  96. lastPos = pos + 1;
  97. }
  98. return tokens;
  99. }
  100. String String::replace(const String &what, const String &withWhat) const {
  101. vector<String> arr = split(what);
  102. String retString = "";
  103. for(int i= 0; i < arr.size(); i++) {
  104. retString += arr[i];
  105. if(i < arr.size()-1)
  106. retString += withWhat;
  107. }
  108. return retString;
  109. }
  110. String String::toLowerCase() const {
  111. wstring str = contents;
  112. std::transform(str.begin(), str.end(), str.begin(),::tolower);
  113. return String(str);
  114. }
  115. String String::toUpperCase() const {
  116. wstring str = contents;
  117. std::transform(str.begin(), str.end(), str.begin(),::toupper);
  118. return String(str);
  119. }
  120. String String::NumberToString(Number value) {
  121. char temp[128];
  122. sprintf(temp, "%f", value);
  123. return String(temp);
  124. }
  125. const string& String::getSTLString() {
  126. s_contents.assign(contents.begin(),contents.end());
  127. return s_contents;
  128. }
  129. const wstring& String::getSTLWString() const {
  130. return contents;
  131. }
  132. const char *String::c_str() {
  133. s_contents.assign(contents.begin(),contents.end());
  134. return s_contents.c_str();
  135. }
  136. const wchar_t *String::wc_str() const {
  137. return contents.c_str();
  138. }
  139. void utf8toWStr(WStr& dest, const Str& src){
  140. dest.clear();
  141. wchar_t w = 0;
  142. int bytes = 0;
  143. wchar_t err = L'�';
  144. for (size_t i = 0; i < src.size(); i++){
  145. unsigned char c = (unsigned char)src[i];
  146. if (c <= 0x7f){//first byte
  147. if (bytes){
  148. dest.push_back(err);
  149. bytes = 0;
  150. }
  151. dest.push_back((wchar_t)c);
  152. }
  153. else if (c <= 0xbf){//second/third/etc byte
  154. if (bytes){
  155. w = ((w << 6)|(c & 0x3f));
  156. bytes--;
  157. if (bytes == 0)
  158. dest.push_back(w);
  159. }
  160. else
  161. dest.push_back(err);
  162. }
  163. else if (c <= 0xdf){//2byte sequence start
  164. bytes = 1;
  165. w = c & 0x1f;
  166. }
  167. else if (c <= 0xef){//3byte sequence start
  168. bytes = 2;
  169. w = c & 0x0f;
  170. }
  171. else if (c <= 0xf7){//3byte sequence start
  172. bytes = 3;
  173. w = c & 0x07;
  174. }
  175. else{
  176. dest.push_back(err);
  177. bytes = 0;
  178. }
  179. }
  180. if (bytes)
  181. dest.push_back(err);
  182. }
  183. void wstrToUtf8(Str& dest, const WStr& src){
  184. dest.clear();
  185. for (size_t i = 0; i < src.size(); i++){
  186. wchar_t w = src[i];
  187. if (w <= 0x7f)
  188. dest.push_back((char)w);
  189. else if (w <= 0x7ff){
  190. dest.push_back(0xc0 | ((w >> 6)& 0x1f));
  191. dest.push_back(0x80| (w & 0x3f));
  192. }
  193. else if (w <= 0xffff){
  194. dest.push_back(0xe0 | ((w >> 12)& 0x0f));
  195. dest.push_back(0x80| ((w >> 6) & 0x3f));
  196. dest.push_back(0x80| (w & 0x3f));
  197. }
  198. else if (w <= 0x10ffff){
  199. dest.push_back(0xf0 | ((w >> 18)& 0x07));
  200. dest.push_back(0x80| ((w >> 12) & 0x3f));
  201. dest.push_back(0x80| ((w >> 6) & 0x3f));
  202. dest.push_back(0x80| (w & 0x3f));
  203. }
  204. else
  205. dest.push_back('?');
  206. }
  207. }