GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/src/node_i18n.cc at master · lukecameron/node Expand file tree/ Copy pathnode_i18n.ccMore file actions 880 lines (778 loc) · 29.7 KB / Copy pathnode_i18n.ccFile metadata and controls880 lines (778 loc) · 29.7 KB 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 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 // Copyright Joyent, Inc. and other Node contributors. // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to permit // persons to whom the Software is furnished to do so, subject to the // following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. /* * notes: by srl295 * - When in NODE_HAVE_SMALL_ICU mode, ICU is linked against "stub" (null) data * ( stubdata/libicudata.a ) containing nothing, no data, and it's also * linked against a "small" data file which the SMALL_ICUDATA_ENTRY_POINT * macro names. That's the "english+root" data. * * If icu_data_path is non-null, the user has provided a path and we assume * it goes somewhere useful. We set that path in ICU, and exit. * If icu_data_path is null, they haven't set a path and we want the * "english+root" data. We call * udata_setCommonData(SMALL_ICUDATA_ENTRY_POINT,...) * to load up the english+root data. * * - when NOT in NODE_HAVE_SMALL_ICU mode, ICU is linked directly with its full * data. All of the variables and command line options for changing data at * runtime are disabled, as they wouldn't fully override the internal data. * See: http://bugs.icu-project.org/trac/ticket/10924 */ #include "node_i18n.h" #if defined(NODE_HAVE_I18N_SUPPORT) #include "node.h" #include "node_buffer.h" #include "env-inl.h" #include "util-inl.h" #include "base_object-inl.h" #include "v8.h" #include <unicode/utypes.h> #include <unicode/putil.h> #include <unicode/uchar.h> #include <unicode/uclean.h> #include <unicode/udata.h> #include <unicode/uidna.h> #include <unicode/ucnv.h> #include <unicode/utf8.h> #include <unicode/utf16.h> #include <unicode/timezone.h> #include <unicode/ulocdata.h> #include <unicode/uvernum.h> #include <unicode/uversion.h> #include <unicode/ustring.h> #ifdef NODE_HAVE_SMALL_ICU /* if this is defined, we have a 'secondary' entry point. compare following to utypes.h defs for U_ICUDATA_ENTRY_POINT */ #define SMALL_ICUDATA_ENTRY_POINT \ SMALL_DEF2(U_ICU_VERSION_MAJOR_NUM, U_LIB_SUFFIX_C_NAME) #define SMALL_DEF2(major, suff) SMALL_DEF(major, suff) #ifndef U_LIB_SUFFIX_C_NAME #define SMALL_DEF(major, suff) icusmdt##major##_dat #else #define SMALL_DEF(major, suff) icusmdt##suff##major##_dat #endif extern "C" const char U_DATA_API SMALL_ICUDATA_ENTRY_POINT[]; #endif namespace node { using v8::Context; using v8::FunctionCallbackInfo; using v8::HandleScope; using v8::Isolate; using v8::Local; using v8::MaybeLocal; using v8::Object; using v8::ObjectTemplate; using v8::String; using v8::Value; namespace i18n { namespace { template <typename T> MaybeLocal<Object> ToBufferEndian(Environment* env, MaybeStackBuffer<T>* buf) { MaybeLocal<Object> ret = Buffer::New(env, buf); if (ret.IsEmpty()) return ret; static_assert(sizeof(T) == 1 || sizeof(T) == 2, "Currently only one- or two-byte buffers are supported"); if (sizeof(T) > 1 && IsBigEndian()) { SPREAD_BUFFER_ARG(ret.ToLocalChecked(), retbuf); SwapBytes16(retbuf_data, retbuf_length); } return ret; } struct Converter { explicit Converter(const char* name, const char* sub = nullptr) : conv(nullptr) { UErrorCode status = U_ZERO_ERROR; conv = ucnv_open(name, &status); CHECK(U_SUCCESS(status)); if (sub != nullptr) { ucnv_setSubstChars(conv, sub, strlen(sub), &status); } } explicit Converter(UConverter* converter, const char* sub = nullptr) : conv(converter) { CHECK_NE(conv, nullptr); UErrorCode status = U_ZERO_ERROR; if (sub != nullptr) { ucnv_setSubstChars(conv, sub, strlen(sub), &status); } } ~Converter() { ucnv_close(conv); } UConverter* conv; }; class ConverterObject : public BaseObject, Converter { public: enum ConverterFlags { CONVERTER_FLAGS_FLUSH = 0x1, CONVERTER_FLAGS_FATAL = 0x2, CONVERTER_FLAGS_IGNORE_BOM = 0x4 }; ~ConverterObject() override {} static void Has(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); HandleScope scope(env->isolate()); CHECK_GE(args.Length(), 1); Utf8Value label(env->isolate(), args[0]); UErrorCode status = U_ZERO_ERROR; UConverter* conv = ucnv_open(*label, &status); args.GetReturnValue().Set(!!U_SUCCESS(status)); ucnv_close(conv); } static void Create(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); HandleScope scope(env->isolate()); CHECK_GE(args.Length(), 2); Utf8Value label(env->isolate(), args[0]); int flags = args[1]->Uint32Value(env->context()).ToChecked(); bool fatal = (flags & CONVERTER_FLAGS_FATAL) == CONVERTER_FLAGS_FATAL; bool ignoreBOM = (flags & CONVERTER_FLAGS_IGNORE_BOM) == CONVERTER_FLAGS_IGNORE_BOM; UErrorCode status = U_ZERO_ERROR; UConverter* conv = ucnv_open(*label, &status); if (U_FAILURE(status)) return; if (fatal) { status = U_ZERO_ERROR; ucnv_setToUCallBack(conv, UCNV_TO_U_CALLBACK_STOP, nullptr, nullptr, nullptr, &status); } Local<ObjectTemplate> t = ObjectTemplate::New(env->isolate()); t->SetInternalFieldCount(1); Local<Object> obj = t->NewInstance(env->context()).ToLocalChecked(); new ConverterObject(env, obj, conv, ignoreBOM); args.GetReturnValue().Set(obj); } static void Decode(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); CHECK_GE(args.Length(), 3); // Converter, Buffer, Flags Converter utf8("utf8"); ConverterObject* converter; ASSIGN_OR_RETURN_UNWRAP(&converter, args[0].As<Object>()); SPREAD_BUFFER_ARG(args[1], input_obj); int flags = args[2]->Uint32Value(env->context()).ToChecked(); UErrorCode status = U_ZERO_ERROR; MaybeStackBuffer<UChar> result; MaybeLocal<Object> ret; size_t limit = ucnv_getMinCharSize(converter->conv) * input_obj_length; if (limit > 0) result.AllocateSufficientStorage(limit); UBool flush = (flags & CONVERTER_FLAGS_FLUSH) == CONVERTER_FLAGS_FLUSH; const char* source = input_obj_data; size_t source_length = input_obj_length; if (converter->unicode_ && !converter->ignoreBOM_ && !converter->bomSeen_) { int32_t bomOffset = 0; ucnv_detectUnicodeSignature(source, source_length, &bomOffset, &status); source += bomOffset; source_length -= bomOffset; converter->bomSeen_ = true; } UChar* target = *result; ucnv_toUnicode(converter->conv, &target, target + (limit * sizeof(UChar)), &source, source + source_length, nullptr, flush, &status); if (U_SUCCESS(status)) { if (limit > 0) result.SetLength(target - &result[0]); ret = ToBufferEndian(env, &result); args.GetReturnValue().Set(ret.ToLocalChecked()); goto reset; } args.GetReturnValue().Set(status); reset: if (flush) { // Reset the converter state converter->bomSeen_ = false; ucnv_reset(converter->conv); } } protected: ConverterObject(Environment* env, v8::Local<v8::Object> wrap, UConverter* converter, bool ignoreBOM, const char* sub = nullptr) : BaseObject(env, wrap), Converter(converter, sub), ignoreBOM_(ignoreBOM) { MakeWeak<ConverterObject>(this); switch (ucnv_getType(converter)) { case UCNV_UTF8: case UCNV_UTF16_BigEndian: case UCNV_UTF16_LittleEndian: unicode_ = true; break; default: unicode_ = false; } } private: bool unicode_ = false; // True if this is a Unicode converter bool ignoreBOM_ = false; // True if the BOM should be ignored on Unicode bool bomSeen_ = false; // True if the BOM has been seen }; // One-Shot Converters void CopySourceBuffer(MaybeStackBuffer<UChar>* dest, const char* data, const size_t length, const size_t length_in_chars) { dest->AllocateSufficientStorage(length_in_chars); char* dst = reinterpret_cast<char*>(**dest); memcpy(dst, data, length); if (IsBigEndian()) { SwapBytes16(dst, length); } } typedef MaybeLocal<Object> (*TranscodeFunc)(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status); MaybeLocal<Object> Transcode(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status) { *status = U_ZERO_ERROR; MaybeLocal<Object> ret; MaybeStackBuffer<char> result; Converter to(toEncoding, "?"); Converter from(fromEncoding); const uint32_t limit = source_length * ucnv_getMaxCharSize(to.conv); result.AllocateSufficientStorage(limit); char* target = *result; ucnv_convertEx(to.conv, from.conv, &target, target + limit, &source, source + source_length, nullptr, nullptr, nullptr, nullptr, true, true, status); if (U_SUCCESS(*status)) { result.SetLength(target - &result[0]); ret = ToBufferEndian(env, &result); } return ret; } MaybeLocal<Object> TranscodeToUcs2(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status) { *status = U_ZERO_ERROR; MaybeLocal<Object> ret; MaybeStackBuffer<UChar> destbuf(source_length); Converter from(fromEncoding); const size_t length_in_chars = source_length * sizeof(UChar); ucnv_toUChars(from.conv, *destbuf, length_in_chars, source, source_length, status); if (U_SUCCESS(*status)) ret = ToBufferEndian(env, &destbuf); return ret; } MaybeLocal<Object> TranscodeFromUcs2(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status) { *status = U_ZERO_ERROR; MaybeStackBuffer<UChar> sourcebuf; MaybeLocal<Object> ret; Converter to(toEncoding, "?"); const size_t length_in_chars = source_length / sizeof(UChar); CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars); MaybeStackBuffer<char> destbuf(length_in_chars); const uint32_t len = ucnv_fromUChars(to.conv, *destbuf, length_in_chars, *sourcebuf, length_in_chars, status); if (U_SUCCESS(*status)) { destbuf.SetLength(len); ret = ToBufferEndian(env, &destbuf); } return ret; } MaybeLocal<Object> TranscodeUcs2FromUtf8(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status) { *status = U_ZERO_ERROR; MaybeStackBuffer<UChar> destbuf; int32_t result_length; u_strFromUTF8(*destbuf, destbuf.capacity(), &result_length, source, source_length, status); MaybeLocal<Object> ret; if (U_SUCCESS(*status)) { destbuf.SetLength(result_length); ret = ToBufferEndian(env, &destbuf); } else if (*status == U_BUFFER_OVERFLOW_ERROR) { *status = U_ZERO_ERROR; destbuf.AllocateSufficientStorage(result_length); u_strFromUTF8(*destbuf, result_length, &result_length, source, source_length, status); if (U_SUCCESS(*status)) { destbuf.SetLength(result_length); ret = ToBufferEndian(env, &destbuf); } } return ret; } MaybeLocal<Object> TranscodeUtf8FromUcs2(Environment* env, const char* fromEncoding, const char* toEncoding, const char* source, const size_t source_length, UErrorCode* status) { *status = U_ZERO_ERROR; MaybeLocal<Object> ret; const size_t length_in_chars = source_length / sizeof(UChar); int32_t result_length; MaybeStackBuffer<UChar> sourcebuf; MaybeStackBuffer<char> destbuf; CopySourceBuffer(&sourcebuf, source, source_length, length_in_chars); u_strToUTF8(*destbuf, destbuf.capacity(), &result_length, *sourcebuf, length_in_chars, status); if (U_SUCCESS(*status)) { destbuf.SetLength(result_length); ret = ToBufferEndian(env, &destbuf); } else if (*status == U_BUFFER_OVERFLOW_ERROR) { *status = U_ZERO_ERROR; destbuf.AllocateSufficientStorage(result_length); u_strToUTF8(*destbuf, result_length, &result_length, *sourcebuf, length_in_chars, status); if (U_SUCCESS(*status)) { destbuf.SetLength(result_length); ret = ToBufferEndian(env, &destbuf); } } return ret; } const char* EncodingName(const enum encoding encoding) { switch (encoding) { case ASCII: return "us-ascii"; case LATIN1: return "iso8859-1"; case UCS2: return "utf16le"; case UTF8: return "utf-8"; default: return nullptr; } } bool SupportedEncoding(const enum encoding encoding) { switch (encoding) { case ASCII: case LATIN1: case UCS2: case UTF8: return true; default: return false; } } void Transcode(const FunctionCallbackInfo<Value>&args) { Environment* env = Environment::GetCurrent(args); Isolate* isolate = env->isolate(); UErrorCode status = U_ZERO_ERROR; MaybeLocal<Object> result; THROW_AND_RETURN_UNLESS_BUFFER(env, args[0]); SPREAD_BUFFER_ARG(args[0], ts_obj); const enum encoding fromEncoding = ParseEncoding(isolate, args[1], BUFFER); const enum encoding toEncoding = ParseEncoding(isolate, args[2], BUFFER); if (SupportedEncoding(fromEncoding) && SupportedEncoding(toEncoding)) { TranscodeFunc tfn = &Transcode; switch (fromEncoding) { case ASCII: case LATIN1: if (toEncoding == UCS2) tfn = &TranscodeToUcs2; break; case UTF8: if (toEncoding == UCS2) tfn = &TranscodeUcs2FromUtf8; break; case UCS2: switch (toEncoding) { case UCS2: tfn = &Transcode; break; case UTF8: tfn = &TranscodeUtf8FromUcs2; break; default: tfn = &TranscodeFromUcs2; } break; default: // This should not happen because of the SupportedEncoding checks ABORT(); } result = tfn(env, EncodingName(fromEncoding), EncodingName(toEncoding), ts_obj_data, ts_obj_length, &status); } else { status = U_ILLEGAL_ARGUMENT_ERROR; } if (result.IsEmpty()) return args.GetReturnValue().Set(status); return args.GetReturnValue().Set(result.ToLocalChecked()); } void ICUErrorName(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); UErrorCode status = static_cast<UErrorCode>(args[0]->Int32Value()); args.GetReturnValue().Set( String::NewFromUtf8(env->isolate(), u_errorName(status), v8::NewStringType::kNormal).ToLocalChecked()); } #define TYPE_ICU "icu" #define TYPE_UNICODE "unicode" #define TYPE_CLDR "cldr" #define TYPE_TZ "tz" /** * This is the workhorse function that deals with the actual version info. * Get an ICU version. * @param type the type of version to get. One of VERSION_TYPES * @param buf optional buffer for result * @param status ICU error status. If failure, assume result is undefined. * @return version number, or NULL. May or may not be buf. */ const char* GetVersion(const char* type, char buf[U_MAX_VERSION_STRING_LENGTH], UErrorCode* status) { if (!strcmp(type, TYPE_ICU)) { return U_ICU_VERSION; } else if (!strcmp(type, TYPE_UNICODE)) { return U_UNICODE_VERSION; } else if (!strcmp(type, TYPE_TZ)) { return TimeZone::getTZDataVersion(*status); } else if (!strcmp(type, TYPE_CLDR)) { UVersionInfo versionArray; ulocdata_getCLDRVersion(versionArray, status); if (U_SUCCESS(*status)) { u_versionToString(versionArray, buf); return buf; } } // Fall through - unknown type or error case return nullptr; } void GetVersion(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if ( args.Length() == 0 ) { // With no args - return a comma-separated list of allowed values args.GetReturnValue().Set( String::NewFromUtf8(env->isolate(), TYPE_ICU "," TYPE_UNICODE "," TYPE_CLDR "," TYPE_TZ)); } else { CHECK_GE(args.Length(), 1); CHECK(args[0]->IsString()); Utf8Value val(env->isolate(), args[0]); UErrorCode status = U_ZERO_ERROR; char buf[U_MAX_VERSION_STRING_LENGTH] = ""; // Possible output buffer. const char* versionString = GetVersion(*val, buf, &status); if (U_SUCCESS(status) && versionString) { // Success. args.GetReturnValue().Set( String::NewFromUtf8(env->isolate(), versionString)); } } } } // anonymous namespace bool InitializeICUDirectory(const std::string& path) { UErrorCode status = U_ZERO_ERROR; if (path.empty()) { #ifdef NODE_HAVE_SMALL_ICU // install the 'small' data. udata_setCommonData(&SMALL_ICUDATA_ENTRY_POINT, &status); #else // !NODE_HAVE_SMALL_ICU // no small data, so nothing to do. #endif // !NODE_HAVE_SMALL_ICU } else { u_setDataDirectory(path.c_str()); u_init(&status); } return status == U_ZERO_ERROR; } int32_t ToUnicode(MaybeStackBuffer<char>* buf, const char* input, size_t length) { UErrorCode status = U_ZERO_ERROR; uint32_t options = UIDNA_NONTRANSITIONAL_TO_UNICODE; UIDNA* uidna = uidna_openUTS46(options, &status); if (U_FAILURE(status)) return -1; UIDNAInfo info = UIDNA_INFO_INITIALIZER; int32_t len = uidna_nameToUnicodeUTF8(uidna, input, length, **buf, buf->capacity(), &info, &status); // Do not check info.errors like we do with ToASCII since ToUnicode always // returns a string, despite any possible errors that may have occurred. if (status == U_BUFFER_OVERFLOW_ERROR) { status = U_ZERO_ERROR; buf->AllocateSufficientStorage(len); len = uidna_nameToUnicodeUTF8(uidna, input, length, **buf, buf->capacity(), &info, &status); } // info.errors is ignored as UTS #46 ToUnicode always produces a Unicode // string, regardless of whether an error occurred. if (U_FAILURE(status)) { len = -1; buf->SetLength(0); } else { buf->SetLength(len); } uidna_close(uidna); return len; } int32_t ToASCII(MaybeStackBuffer<char>* buf, const char* input, size_t length, enum idna_mode mode) { UErrorCode status = U_ZERO_ERROR; uint32_t options = // CheckHyphens = false; handled later UIDNA_CHECK_BIDI | // CheckBidi = true UIDNA_CHECK_CONTEXTJ | // CheckJoiners = true UIDNA_NONTRANSITIONAL_TO_ASCII; // Nontransitional_Processing if (mode == IDNA_STRICT) { options |= UIDNA_USE_STD3_RULES; // UseSTD3ASCIIRules = beStrict // VerifyDnsLength = beStrict; // handled later } UIDNA* uidna = uidna_openUTS46(options, &status); if (U_FAILURE(status)) return -1; UIDNAInfo info = UIDNA_INFO_INITIALIZER; int32_t len = uidna_nameToASCII_UTF8(uidna, input, length, **buf, buf->capacity(), &info, &status); if (status == U_BUFFER_OVERFLOW_ERROR) { status = U_ZERO_ERROR; buf->AllocateSufficientStorage(len); len = uidna_nameToASCII_UTF8(uidna, input, length, **buf, buf->capacity(), &info, &status); } // In UTS #46 which specifies ToASCII, certain error conditions are // configurable through options, and the WHATWG URL Standard promptly elects // to disable some of them to accommodate for real-world use cases. // Unfortunately, ICU4C's IDNA module does not support disabling some of // these options through `options` above, and thus continues throwing // unnecessary errors. To counter this situation, we just filter out the // errors that may have happened afterwards, before deciding whether to // return an error from this function. // CheckHyphens = false // (Specified in the current UTS #46 draft rev. 18.) // Refs: // - https://github.com/whatwg/url/issues/53 // - https://github.com/whatwg/url/pull/309 // - http://www.unicode.org/review/pri317/ // - http://www.unicode.org/reports/tr46/tr46-18.html // - https://www.icann.org/news/announcement-2000-01-07-en info.errors &= ~UIDNA_ERROR_HYPHEN_3_4; info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN; info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN; if (mode != IDNA_STRICT) { // VerifyDnsLength = beStrict info.errors &= ~UIDNA_ERROR_EMPTY_LABEL; info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG; info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG; } if (U_FAILURE(status) || (mode != IDNA_LENIENT && info.errors != 0)) { len = -1; buf->SetLength(0); } else { buf->SetLength(len); } uidna_close(uidna); return len; } static void ToUnicode(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); CHECK_GE(args.Length(), 1); CHECK(args[0]->IsString()); Utf8Value val(env->isolate(), args[0]); MaybeStackBuffer<char> buf; int32_t len = ToUnicode(&buf, *val, val.length()); if (len < 0) { return env->ThrowError("Cannot convert name to Unicode"); } args.GetReturnValue().Set( String::NewFromUtf8(env->isolate(), *buf, v8::NewStringType::kNormal, len).ToLocalChecked()); } static void ToASCII(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); CHECK_GE(args.Length(), 1); CHECK(args[0]->IsString()); Utf8Value val(env->isolate(), args[0]); // optional arg bool lenient = args[1]->BooleanValue(env->context()).FromJust(); enum idna_mode mode = lenient ? IDNA_LENIENT : IDNA_DEFAULT; MaybeStackBuffer<char> buf; int32_t len = ToASCII(&buf, *val, val.length(), mode); if (len < 0) { return env->ThrowError("Cannot convert name to ASCII"); } args.GetReturnValue().Set( String::NewFromUtf8(env->isolate(), *buf, v8::NewStringType::kNormal, len).ToLocalChecked()); } // This is similar to wcwidth except that it takes the current unicode // character properties database into consideration, allowing it to // correctly calculate the column widths of things like emoji's and // newer wide characters. wcwidth, on the other hand, uses a fixed // algorithm that does not take things like emoji into proper // consideration. // // TODO(TimothyGu): Investigate Cc (C0/C1 control codes). Both VTE (used by // GNOME Terminal) and Konsole don't consider them to be zero-width (see refs // below), and when printed in VTE it is Narrow. However GNOME Terminal doesn't // allow it to be input. Linux's PTY terminal prints control characters as // Narrow rhombi. // // TODO(TimothyGu): Investigate Hangul jamo characters. Medial vowels and final // consonants are 0-width when combined with initial consonants; otherwise they // are technically Wide. But many terminals (including Konsole and // VTE/GLib-based) implement all medials and finals as 0-width. // // Refs: https://eev.ee/blog/2015/09/12/dark-corners-of-unicode/#combining-characters-and-character-width // Refs: https://github.com/GNOME/glib/blob/79e4d4c6be/glib/guniprop.c#L388-L420 // Refs: https://github.com/KDE/konsole/blob/8c6a5d13c0/src/konsole_wcwidth.cpp#L101-L223 static int GetColumnWidth(UChar32 codepoint, bool ambiguous_as_full_width = false) { const auto zero_width_mask = U_GC_CC_MASK | // C0/C1 control code U_GC_CF_MASK | // Format control character U_GC_ME_MASK | // Enclosing mark U_GC_MN_MASK; // Nonspacing mark if (codepoint != 0x00AD && // SOFT HYPHEN is Cf but not zero-width ((U_MASK(u_charType(codepoint)) & zero_width_mask) || u_hasBinaryProperty(codepoint, UCHAR_EMOJI_MODIFIER))) { return 0; } // UCHAR_EAST_ASIAN_WIDTH is the Unicode property that identifies a // codepoint as being full width, wide, ambiguous, neutral, narrow, // or halfwidth. const int eaw = u_getIntPropertyValue(codepoint, UCHAR_EAST_ASIAN_WIDTH); switch (eaw) { case U_EA_FULLWIDTH: case U_EA_WIDE: return 2; case U_EA_AMBIGUOUS: // See: http://www.unicode.org/reports/tr11/#Ambiguous for details if (ambiguous_as_full_width) { return 2; } // If ambiguous_as_full_width is false: // Fall through case U_EA_NEUTRAL: if (u_hasBinaryProperty(codepoint, UCHAR_EMOJI_PRESENTATION)) { return 2; } // Fall through case U_EA_HALFWIDTH: case U_EA_NARROW: default: return 1; } } // Returns the column width for the given String. static void GetStringWidth(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return; bool ambiguous_as_full_width = args[1]->BooleanValue(); bool expand_emoji_sequence = args[2]->BooleanValue(); if (args[0]->IsNumber()) { args.GetReturnValue().Set( GetColumnWidth(args[0]->Uint32Value(), ambiguous_as_full_width)); return; } TwoByteValue value(env->isolate(), args[0]); // reinterpret_cast is required by windows to compile UChar* str = reinterpret_cast<UChar*>(*value); static_assert(sizeof(*str) == sizeof(**value), "sizeof(*str) == sizeof(**value)"); UChar32 c = 0; UChar32 p; size_t n = 0; uint32_t width = 0; while (n < value.length()) { p = c; U16_NEXT(str, n, value.length(), c); // Don't count individual emoji codepoints that occur within an // emoji sequence. This is not necessarily foolproof. Some // environments display emoji sequences in the appropriate // condensed form (as a single emoji glyph), other environments // may not understand an emoji sequence and will display each // individual emoji separately. When this happens, the width // calculated will be off, and there's no reliable way of knowing // in advance if a particular sequence is going to be supported. // The expand_emoji_sequence option allows the caller to skip this // check and count each code within an emoji sequence separately. if (!expand_emoji_sequence && n > 0 && p == 0x200d && // 0x200d == ZWJ (zero width joiner) (u_hasBinaryProperty(c, UCHAR_EMOJI_PRESENTATION) || u_hasBinaryProperty(c, UCHAR_EMOJI_MODIFIER))) { continue; } width += GetColumnWidth(c, ambiguous_as_full_width); } args.GetReturnValue().Set(width); } void Init(Local<Object> target, Local<Value> unused, Local<Context> context, void* priv) { Environment* env = Environment::GetCurrent(context); env->SetMethod(target, "toUnicode", ToUnicode); env->SetMethod(target, "toASCII", ToASCII); env->SetMethod(target, "getStringWidth", GetStringWidth); env->SetMethod(target, "getVersion", GetVersion); // One-shot converters env->SetMethod(target, "icuErrName", ICUErrorName); env->SetMethod(target, "transcode", Transcode); // ConverterObject env->SetMethod(target, "getConverter", ConverterObject::Create); env->SetMethod(target, "decode", ConverterObject::Decode); env->SetMethod(target, "hasConverter", ConverterObject::Has); } } // namespace i18n } // namespace node NODE_BUILTIN_MODULE_CONTEXT_AWARE(icu, node::i18n::Init) #endif // NODE_HAVE_I18N_SUPPORT |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |