字符编码转换
文章转自王牌软件
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
只回答业务咨询
站长推荐:NSetup一键部署软件
一键式完成美化安装包制作,自动增量升级,数据统计,数字签名。应对各种复杂场景,脚本模块化拆分,常规复杂的脚本代码,图形化设置。无需专业的研发经验,轻松完成项目部署。(www.nsetup.cn)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
#ifndef _FACE_STRING_H_ #define _FACE_STRING_H_ #pragma once namespace Face { using namespace Face; class FACE_API FaceString : public Face::FaceObject { private: static const wchar_t zero = 0; mutable wchar_t* buffer; mutable volatile fint* counter; mutable fint start; mutable fint length; mutable fint realLength; static fint CalculateLength(const wchar_t* buffer) { fint result = 0; while (*buffer++) result++; return result; } static fint Compare(const wchar_t* bufA, const FaceString& strB) { const wchar_t* bufB = strB.buffer + strB.start; const wchar_t* bufAOld = bufA; fint length = strB.length; while (length-- && *bufA) { fint diff = *bufA++ - *bufB++; if (diff != 0) { return diff; } }; return CalculateLength(bufAOld) - strB.length; } public: static fint Compare(const FaceString& strA, const FaceString& strB) { const wchar_t* bufA = strA.buffer + strA.start; const wchar_t* bufB = strB.buffer + strB.start; fint length = strA.length < strB.length ? strA.length : strB.length; while (length--) { fint diff = *bufA++ - *bufB++; if (diff != 0) { return diff; } }; return strA.length - strB.length; } private: void Inc()const { if (counter) { INCRC(counter); } } void Dec()const { if (counter) { if (DECRC(counter) == 0) { delete[] buffer; delete counter; } } } FaceString(const FaceString& string, fint _start, fint _length) { if (_length <= 0) { buffer = (wchar_t*)&zero; counter = 0; start = 0; length = 0; realLength = 0; } else { buffer = string.buffer; counter = string.counter; start = string.start + _start; length = _length; realLength = string.realLength; Inc(); } } FaceString(const FaceString& dest, const FaceString& source, fint index, fint count) { if (index == 0 && count == dest.length && source.length == 0) { buffer = (wchar_t*)&zero; counter = 0; start = 0; length = 0; realLength = 0; } else { counter = new fint(1); start = 0; length = dest.length - count + source.length; realLength = length; buffer = new wchar_t[length + 1]; memcpy(buffer, dest.buffer + dest.start, sizeof(wchar_t)*index); memcpy(buffer + index, source.buffer + source.start, sizeof(wchar_t)*source.length); memcpy(buffer + index + source.length, (dest.buffer + dest.start + index + count), sizeof(wchar_t)*(dest.length - index - count)); buffer[length] = 0; } } public: static FaceString Empty; FaceString() { buffer = (wchar_t*)&zero; counter = 0; start = 0; length = 0; realLength = 0; } FaceString(const wchar_t& _char) { counter = new fint(1); start = 0; length = 1; buffer = new wchar_t[2]; buffer[0] = _char; buffer[1] = 0; realLength = length; } FaceString(const wchar_t* _buffer, fint _length) { if (_length <= 0) { buffer = (wchar_t*)&zero; counter = 0; start = 0; length = 0; realLength = 0; } else { buffer = new wchar_t[_length + 1]; memcpy(buffer, _buffer, _length*sizeof(wchar_t)); buffer[_length] = 0; counter = new fint(1); start = 0; length = _length; realLength = _length; } } FaceString(const wchar_t* _buffer, bool copy = true) { CHECK_ERROR(_buffer != 0, L"FaceString::FaceString(const wchar_t*, bool)#Cannot construct a string from nullptr."); if (copy) { counter = new fint(1); start = 0; length = CalculateLength(_buffer); buffer = new wchar_t[length + 1]; memcpy(buffer, _buffer, sizeof(wchar_t)*(length + 1)); realLength = length; } else { buffer = (wchar_t*)_buffer; counter = 0; start = 0; length = CalculateLength(_buffer); realLength = length; } } FaceString(const FaceString& string) { buffer = string.buffer; counter = string.counter; start = string.start; length = string.length; realLength = string.realLength; Inc(); } FaceString(FaceString&& string) { buffer = string.buffer; counter = string.counter; start = string.start; length = string.length; realLength = string.realLength; string.buffer = (wchar_t*)&zero; string.counter = 0; string.start = 0; string.length = 0; string.realLength = 0; } ~FaceString() { Dec(); } const wchar_t* Buffer()const { if (start + length != realLength) { wchar_t* newBuffer = new wchar_t[length + 1]; memcpy(newBuffer, buffer + start, sizeof(wchar_t)*length); newBuffer[length] = 0; Dec(); buffer = newBuffer; counter = new fint(1); start = 0; realLength = length; } return buffer + start; } FaceString& operator=(const FaceString& string) { if (this != &string) { Dec(); buffer = string.buffer; counter = string.counter; start = string.start; length = string.length; realLength = string.realLength; Inc(); } return *this; } FaceString& operator=(FaceString&& string) { if (this != &string) { Dec(); buffer = string.buffer; counter = string.counter; start = string.start; length = string.length; realLength = string.realLength; string.buffer = (wchar_t*)&zero; string.counter = 0; string.start = 0; string.length = 0; string.realLength = 0; } return *this; } FaceString& operator+=(const FaceString& string) { return *this = *this + string; } FaceString operator+(const FaceString& string)const { return FaceString(*this, string, length, 0); } bool operator==(const FaceString& string)const { return Compare(*this, string) == 0; } bool operator!=(const FaceString& string)const { return Compare(*this, string) != 0; } bool operator>(const FaceString& string)const { return Compare(*this, string) > 0; } bool operator>=(const FaceString& string)const { return Compare(*this, string) >= 0; } bool operator<(const FaceString& string)const { return Compare(*this, string) < 0; } bool operator<=(const FaceString& string)const { return Compare(*this, string) <= 0; } bool operator==(const wchar_t* buffer)const { return Compare(buffer, *this) == 0; } bool operator!=(const wchar_t* buffer)const { return Compare(buffer, *this) != 0; } bool operator>(const wchar_t* buffer)const { return Compare(buffer, *this) < 0; } bool operator>=(const wchar_t* buffer)const { return Compare(buffer, *this) <= 0; } bool operator<(const wchar_t* buffer)const { return Compare(buffer, *this)>0; } bool operator<=(const wchar_t* buffer)const { return Compare(buffer, *this) >= 0; } wchar_t operator[](fint index)const { CHECK_ERROR(index >= 0 && index < length, L"FaceString:<wchar_t>:operator[](fint)#Argument index not in range."); return buffer[start + index]; } fint Length()const { return length; } fint IndexOf(wchar_t c)const { const wchar_t* reading = buffer + start; for (fint i = 0; i < length; i++) { if (reading[i] == c) return i; } return -1; } FaceString Left(fint count)const { CHECK_ERROR(count >= 0 && count <= length, L"FaceString::Left(fint)#Argument count not in range."); return FaceString(*this, 0, count); } FaceString Right(fint count)const { CHECK_ERROR(count >= 0 && count <= length, L"FaceString::Right(fint)#Argument count not in range."); return FaceString(*this, length - count, count); } FaceString Sub(fint index, fint count)const { CHECK_ERROR(index >= 0 && index <= length, L"FaceString::Sub(fint, fint)#Argument index not in range."); CHECK_ERROR(index + count >= 0 && index + count <= length, L"FaceString::Sub(fint, fint)#Argument count not in range."); return FaceString(*this, index, count); } FaceString Remove(fint index, fint count)const { CHECK_ERROR(index >= 0 && index < length, L"FaceString::Remove(fint, fint)#Argument index not in range."); CHECK_ERROR(index + count >= 0 && index + count <= length, L"FaceString::Remove(fint, fint)#Argument count not in range."); return FaceString(*this, FaceString(), index, count); } FaceString Insert(fint index, const FaceString& string)const { CHECK_ERROR(index >= 0 && index <= length, L"FaceString::Insert(fint)#Argument count not in range."); return FaceString(*this, string, index, 0); } fint ToInt()const { wchar_t* endptr = 0; fint result = wcstol(this->Buffer(), &endptr, 10); return result; } void Lower() { _wcslwr_s((wchar_t*)this->Buffer(), this->Length() + 1); } void Upper() { _wcsupr_s((wchar_t*)this->Buffer(), this->Length() + 1); } fint Find(const wchar_t c) const { if (c == '\0') return -1; wchar_t* ptr = wcsrchr(buffer, c); if (ptr) { return (fint)(ptr - buffer); } return -1; } fint Find(const wchar_t* sub) const { if (sub == nullptr) return -1; wchar_t* ptr = wcsstr(buffer, sub); if (sub == nullptr) return -1; return (fint)(ptr - buffer); } fint FindLast(const wchar_t c) const { if (c == '\0') return -1; for (fint i = this->Length() - 1; i > 0; i--) { if (buffer[i] == c) return i; } return -1; } fint FindFirst(const wchar_t c) const { if (c == '\0') return -1; for (fint i = 0; i < this->Length(); i++) { if (buffer[i] == c) return i; } return -1; } friend bool operator<(const wchar_t* left, const FaceString& right) { return Compare(left, right)<0; } friend bool operator<=(const wchar_t* left, const FaceString& right) { return Compare(left, right) <= 0; } friend bool operator>(const wchar_t* left, const FaceString& right) { return Compare(left, right) > 0; } friend bool operator>=(const wchar_t* left, const FaceString& right) { return Compare(left, right) >= 0; } friend bool operator==(const wchar_t* left, const FaceString& right) { return Compare(left, right) == 0; } friend bool operator!=(const wchar_t* left, const FaceString& right) { return Compare(left, right) != 0; } friend FaceString operator+(const wchar_t* left, const FaceString& right) { return FaceString(left, false) + right; } static std::wstring A2W(const char* lsz) { int codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; fint iLength = strlen(lsz); fint iCharCount = ::MultiByteToWideChar(codepage, 0, lsz, iLength, nullptr, 0); wchar_t* pW = new wchar_t[iCharCount + 1]; ZeroMemory(pW, (iCharCount + 1) * sizeof(wchar_t)); ::MultiByteToWideChar(codepage, 0, lsz, iLength, (LPWSTR)(pW), iCharCount); std::wstring wstr(pW); delete[] pW; return wstr; } static std::string W2A(const wchar_t* lwsz) { fint iLength = wcslen(lwsz); fint codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP; fint iCharCount = ::WideCharToMultiByte(codepage, 0, lwsz, iLength, nullptr, 0, nullptr, nullptr); char* pA = new char[iCharCount + 1]; ZeroMemory(pA, (iCharCount + 1)* sizeof(char)); ::WideCharToMultiByte(codepage, 0, lwsz, iLength, pA, iCharCount, nullptr, nullptr); std::string str(pA); delete[] pA; return str; } static std::string W2U(const wchar_t* lwsz) { fint iLength = wcslen(lwsz); fint iCharCount = ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)(lwsz), iLength, nullptr, 0, nullptr, nullptr); char* pA = new char[iCharCount + 1]; ZeroMemory(pA, (iCharCount + 1) * sizeof(char)); ::WideCharToMultiByte(CP_UTF8, 0, (LPWSTR)(lwsz), iLength, pA, iCharCount, nullptr, nullptr); std::string str(pA); delete[] pA; return str; } static std::wstring U2W(const char*lsz) { fint iLength = strlen(lsz); fint iCharCount = ::MultiByteToWideChar(CP_UTF8, 0, lsz, iLength, nullptr, 0); wchar_t* pW = new wchar_t[iCharCount + 1]; ZeroMemory(pW, (iCharCount + 1) * sizeof(wchar_t)); ::MultiByteToWideChar(CP_UTF8, 0, lsz, iLength, (LPWSTR)(pW), iCharCount); std::wstring wstr(pW); delete[] pW; return wstr; } }; typedef Face::FaceString WString; } #endif |
学习日记,兼职软件设计,软件修改,毕业设计。
本文出自 学习日记,转载时请注明出处及相应链接。
本文永久链接: https://www.softwareace.cn/?p=1351