uscript.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. **********************************************************************
  5. * Copyright (C) 1997-2014, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. **********************************************************************
  8. *
  9. * File USCRIPT.C
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 07/06/2001 Ram Creation.
  15. ******************************************************************************
  16. */
  17. #include "unicode/uchar.h"
  18. #include "unicode/uscript.h"
  19. #include "unicode/uloc.h"
  20. #include "charstr.h"
  21. #include "cmemory.h"
  22. #include "cstring.h"
  23. #include "ulocimp.h"
  24. static const UScriptCode JAPANESE[3] = { USCRIPT_KATAKANA, USCRIPT_HIRAGANA, USCRIPT_HAN };
  25. static const UScriptCode KOREAN[2] = { USCRIPT_HANGUL, USCRIPT_HAN };
  26. static const UScriptCode HAN_BOPO[2] = { USCRIPT_HAN, USCRIPT_BOPOMOFO };
  27. static int32_t
  28. setCodes(const UScriptCode *src, int32_t length,
  29. UScriptCode *dest, int32_t capacity, UErrorCode *err) {
  30. int32_t i;
  31. if(U_FAILURE(*err)) { return 0; }
  32. if(length > capacity) {
  33. *err = U_BUFFER_OVERFLOW_ERROR;
  34. return length;
  35. }
  36. for(i = 0; i < length; ++i) {
  37. dest[i] = src[i];
  38. }
  39. return length;
  40. }
  41. static int32_t
  42. setOneCode(UScriptCode script, UScriptCode *scripts, int32_t capacity, UErrorCode *err) {
  43. if(U_FAILURE(*err)) { return 0; }
  44. if(1 > capacity) {
  45. *err = U_BUFFER_OVERFLOW_ERROR;
  46. return 1;
  47. }
  48. scripts[0] = script;
  49. return 1;
  50. }
  51. static int32_t
  52. getCodesFromLocale(const char *locale,
  53. UScriptCode *scripts, int32_t capacity, UErrorCode *err) {
  54. if (U_FAILURE(*err)) { return 0; }
  55. icu::CharString lang;
  56. icu::CharString script;
  57. ulocimp_getSubtags(locale, &lang, &script, nullptr, nullptr, nullptr, *err);
  58. if (U_FAILURE(*err)) { return 0; }
  59. // Multi-script languages, equivalent to the LocaleScript data
  60. // that we used to load from locale resource bundles.
  61. if (lang == "ja") {
  62. return setCodes(JAPANESE, UPRV_LENGTHOF(JAPANESE), scripts, capacity, err);
  63. }
  64. if (lang == "ko") {
  65. return setCodes(KOREAN, UPRV_LENGTHOF(KOREAN), scripts, capacity, err);
  66. }
  67. if (lang == "zh" && script == "Hant") {
  68. return setCodes(HAN_BOPO, UPRV_LENGTHOF(HAN_BOPO), scripts, capacity, err);
  69. }
  70. // Explicit script code.
  71. if (!script.isEmpty()) {
  72. UScriptCode scriptCode = static_cast<UScriptCode>(u_getPropertyValueEnum(UCHAR_SCRIPT, script.data()));
  73. if(scriptCode != USCRIPT_INVALID_CODE) {
  74. if(scriptCode == USCRIPT_SIMPLIFIED_HAN || scriptCode == USCRIPT_TRADITIONAL_HAN) {
  75. scriptCode = USCRIPT_HAN;
  76. }
  77. return setOneCode(scriptCode, scripts, capacity, err);
  78. }
  79. }
  80. return 0;
  81. }
  82. /* TODO: this is a bad API and should be deprecated, ticket #11141 */
  83. U_CAPI int32_t U_EXPORT2
  84. uscript_getCode(const char* nameOrAbbrOrLocale,
  85. UScriptCode* fillIn,
  86. int32_t capacity,
  87. UErrorCode* err){
  88. UBool triedCode;
  89. UErrorCode internalErrorCode;
  90. int32_t length;
  91. if(U_FAILURE(*err)) {
  92. return 0;
  93. }
  94. if(nameOrAbbrOrLocale==nullptr ||
  95. (fillIn == nullptr ? capacity != 0 : capacity < 0)) {
  96. *err = U_ILLEGAL_ARGUMENT_ERROR;
  97. return 0;
  98. }
  99. triedCode = false;
  100. const char* lastSepPtr = uprv_strrchr(nameOrAbbrOrLocale, '-');
  101. if (lastSepPtr==nullptr) {
  102. lastSepPtr = uprv_strrchr(nameOrAbbrOrLocale, '_');
  103. }
  104. // Favor interpretation of nameOrAbbrOrLocale as a script alias if either
  105. // 1. nameOrAbbrOrLocale does not contain -/_. Handles Han, Mro, Nko, etc.
  106. // 2. The last instance of -/_ is at offset 3, and the portion after that is
  107. // longer than 4 characters (i.e. not a script or region code). This handles
  108. // Old_Hungarian, Old_Italic, etc. ("old" is a valid language code)
  109. // 3. The last instance of -/_ is at offset 7, and the portion after that is
  110. // 3 characters. This handles New_Tai_Lue ("new" is a valid language code).
  111. if (lastSepPtr==nullptr
  112. || (lastSepPtr-nameOrAbbrOrLocale == 3 && uprv_strlen(nameOrAbbrOrLocale) > 8)
  113. || (lastSepPtr-nameOrAbbrOrLocale == 7 && uprv_strlen(nameOrAbbrOrLocale) == 11) ) {
  114. /* try long and abbreviated script names first */
  115. UScriptCode code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
  116. if(code!=USCRIPT_INVALID_CODE) {
  117. return setOneCode(code, fillIn, capacity, err);
  118. }
  119. triedCode = true;
  120. }
  121. internalErrorCode = U_ZERO_ERROR;
  122. length = getCodesFromLocale(nameOrAbbrOrLocale, fillIn, capacity, err);
  123. if(U_FAILURE(*err) || length != 0) {
  124. return length;
  125. }
  126. icu::CharString likely = ulocimp_addLikelySubtags(nameOrAbbrOrLocale, internalErrorCode);
  127. if(U_SUCCESS(internalErrorCode) && internalErrorCode != U_STRING_NOT_TERMINATED_WARNING) {
  128. length = getCodesFromLocale(likely.data(), fillIn, capacity, err);
  129. if(U_FAILURE(*err) || length != 0) {
  130. return length;
  131. }
  132. }
  133. if(!triedCode) {
  134. /* still not found .. try long and abbreviated script names again */
  135. UScriptCode code = (UScriptCode) u_getPropertyValueEnum(UCHAR_SCRIPT, nameOrAbbrOrLocale);
  136. if(code!=USCRIPT_INVALID_CODE) {
  137. return setOneCode(code, fillIn, capacity, err);
  138. }
  139. }
  140. return 0;
  141. }