PolyString.cpp 6.2 KB

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