PolyString.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. using namespace std;
  22. String::String() {
  23. }
  24. String::String(const wchar_t *str) {
  25. if (str)
  26. wstrToUtf8(contents, str);
  27. }
  28. String::String(const char *str) {
  29. if (str)
  30. contents = str;
  31. }
  32. String::String(const char *str, size_t n) {
  33. if (str)
  34. contents = string(str, n);
  35. }
  36. String::String(const string& str) {
  37. contents = str;
  38. }
  39. String::String(const wstring& str) {
  40. wstrToUtf8(contents, str);
  41. }
  42. String::String(const wchar_t wchar) {
  43. std::wstring srcstr;
  44. srcstr = srcstr + wchar;
  45. std::string newstr;
  46. wstrToUtf8(newstr, srcstr);
  47. contents = contents + newstr;
  48. }
  49. String::~String() {
  50. }
  51. size_t String::getDataSizeWithEncoding(int encoding) const {
  52. switch(encoding) {
  53. case ENCODING_UTF8: {
  54. return contents.size();
  55. }
  56. default:
  57. return NULL;
  58. }
  59. }
  60. const char *String::getDataWithEncoding(int encoding) const {
  61. switch(encoding) {
  62. case ENCODING_UTF8: {
  63. return contents.c_str();
  64. }
  65. break;
  66. default:
  67. return NULL;
  68. }
  69. }
  70. wchar_t *String::getWDataWithEncoding(int encoding) {
  71. switch(encoding) {
  72. case ENCODING_UTF8: {
  73. utf8toWStr(w_contents, contents);
  74. return (wchar_t *)w_contents.c_str();
  75. }
  76. break;
  77. default:
  78. return NULL;
  79. }
  80. }
  81. void String::setDataWithEncoding(char *data, int encoding) {
  82. switch(encoding) {
  83. case ENCODING_UTF8: {
  84. if (data)
  85. contents = data;
  86. else
  87. contents = std::string();
  88. }
  89. default:
  90. break;
  91. }
  92. }
  93. bool String::isNumber() {
  94. std::string::const_iterator it = contents.begin();
  95. while (it != contents.end() && std::isdigit(*it)) ++it;
  96. return !contents.empty() && it == contents.end();
  97. }
  98. vector<String> String::split(const String &delim) const {
  99. vector<String> tokens;
  100. bool trimEmpty = false;
  101. std::string::size_type pos, lastPos = 0;
  102. while(true)
  103. {
  104. pos = contents.find_first_of(delim.contents, lastPos);
  105. if(pos == std::string::npos)
  106. {
  107. pos = contents.length();
  108. if(pos != lastPos || !trimEmpty)
  109. tokens.push_back(vector<String>::value_type(contents.data()+lastPos, (string::size_type)pos-lastPos ));
  110. break;
  111. }
  112. else
  113. {
  114. if(pos != lastPos || !trimEmpty)
  115. tokens.push_back(vector<String>::value_type(contents.data()+lastPos, (string::size_type)pos-lastPos ));
  116. }
  117. lastPos = pos + 1;
  118. }
  119. return tokens;
  120. }
  121. String String::replace(const String &what, const String &withWhat) const {
  122. size_t pos = 0;
  123. std::string retString = contents;
  124. while((pos = retString.find(what.contents, pos)) != std::string::npos) {
  125. retString.replace(pos, what.length(), withWhat.contents);
  126. pos += withWhat.length();
  127. }
  128. return retString;
  129. }
  130. String String::toLowerCase() const {
  131. string str = contents;
  132. std::transform(str.begin(), str.end(), str.begin(),::tolower);
  133. return String(str);
  134. }
  135. String String::toUpperCase() const {
  136. string str = contents;
  137. std::transform(str.begin(), str.end(), str.begin(),::toupper);
  138. return String(str);
  139. }
  140. String String::NumberToString(Number value) {
  141. char temp[128];
  142. sprintf(temp, "%.2f", value);
  143. return String(temp);
  144. }
  145. String String::IntToString(int value) {
  146. char temp[128];
  147. sprintf(temp, "%d", value);
  148. return String(temp);
  149. }
  150. const string& String::getSTLString() const {
  151. return contents;
  152. }
  153. const char *String::c_str() const {
  154. return contents.c_str();
  155. }
  156. void String::append(const char c) {
  157. contents.append(1,c);
  158. }
  159. void utf8toWStr(WStr& dest, const Str& src){
  160. dest.clear();
  161. wchar_t w = 0;
  162. int bytes = 0;
  163. wchar_t err = L'�';
  164. for (size_t i = 0; i < src.size(); i++){
  165. unsigned char c = (unsigned char)src[i];
  166. if (c <= 0x7f){//first byte
  167. if (bytes){
  168. dest.push_back(err);
  169. bytes = 0;
  170. }
  171. dest.push_back((wchar_t)c);
  172. }
  173. else if (c <= 0xbf){//second/third/etc byte
  174. if (bytes){
  175. w = ((w << 6)|(c & 0x3f));
  176. bytes--;
  177. if (bytes == 0)
  178. dest.push_back(w);
  179. }
  180. else
  181. dest.push_back(err);
  182. }
  183. else if (c <= 0xdf){//2byte sequence start
  184. bytes = 1;
  185. w = c & 0x1f;
  186. }
  187. else if (c <= 0xef){//3byte sequence start
  188. bytes = 2;
  189. w = c & 0x0f;
  190. }
  191. else if (c <= 0xf7){//3byte sequence start
  192. bytes = 3;
  193. w = c & 0x07;
  194. }
  195. else{
  196. dest.push_back(err);
  197. bytes = 0;
  198. }
  199. }
  200. if (bytes)
  201. dest.push_back(err);
  202. }
  203. void wstrToUtf8(Str& dest, const WStr& src){
  204. dest.clear();
  205. for (size_t i = 0; i < src.size(); i++){
  206. wchar_t w = src[i];
  207. if (w <= 0x7f)
  208. dest.push_back((char)w);
  209. else if (w <= 0x7ff){
  210. dest.push_back(0xc0 | ((w >> 6)& 0x1f));
  211. dest.push_back(0x80| (w & 0x3f));
  212. }
  213. else if (w <= 0xffff){
  214. dest.push_back(0xe0 | ((w >> 12)& 0x0f));
  215. dest.push_back(0x80| ((w >> 6) & 0x3f));
  216. dest.push_back(0x80| (w & 0x3f));
  217. }
  218. else if (w <= 0x10ffff){
  219. dest.push_back(0xf0 | ((w >> 18)& 0x07));
  220. dest.push_back(0x80| ((w >> 12) & 0x3f));
  221. dest.push_back(0x80| ((w >> 6) & 0x3f));
  222. dest.push_back(0x80| (w & 0x3f));
  223. }
  224. else
  225. dest.push_back('?');
  226. }
  227. }