standardplural.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2015, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. *******************************************************************************
  8. * standardplural.h
  9. *
  10. * created on: 2015dec14
  11. * created by: Markus W. Scherer
  12. */
  13. #ifndef __STANDARDPLURAL_H__
  14. #define __STANDARDPLURAL_H__
  15. #include "unicode/utypes.h"
  16. #if !UCONFIG_NO_FORMATTING
  17. U_NAMESPACE_BEGIN
  18. class UnicodeString;
  19. /**
  20. * Standard CLDR plural form/category constants.
  21. * See http://www.unicode.org/reports/tr35/tr35-numbers.html#Language_Plural_Rules
  22. */
  23. class U_I18N_API StandardPlural {
  24. public:
  25. enum Form {
  26. ZERO,
  27. ONE,
  28. TWO,
  29. FEW,
  30. MANY,
  31. OTHER,
  32. COUNT
  33. };
  34. /**
  35. * @return the lowercase CLDR keyword string for the plural form
  36. */
  37. static const char *getKeyword(Form p);
  38. /**
  39. * @param keyword for example "few" or "other"
  40. * @return the plural form corresponding to the keyword, or OTHER
  41. */
  42. static Form orOtherFromString(const char *keyword) {
  43. return static_cast<Form>(indexOrOtherIndexFromString(keyword));
  44. }
  45. /**
  46. * @param keyword for example "few" or "other"
  47. * @return the plural form corresponding to the keyword, or OTHER
  48. */
  49. static Form orOtherFromString(const UnicodeString &keyword) {
  50. return static_cast<Form>(indexOrOtherIndexFromString(keyword));
  51. }
  52. /**
  53. * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
  54. *
  55. * @param keyword for example "few" or "other"
  56. * @return the plural form corresponding to the keyword
  57. */
  58. static Form fromString(const char *keyword, UErrorCode &errorCode) {
  59. return static_cast<Form>(indexFromString(keyword, errorCode));
  60. }
  61. /**
  62. * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
  63. *
  64. * @param keyword for example "few" or "other"
  65. * @return the plural form corresponding to the keyword
  66. */
  67. static Form fromString(const UnicodeString &keyword, UErrorCode &errorCode) {
  68. return static_cast<Form>(indexFromString(keyword, errorCode));
  69. }
  70. /**
  71. * @param keyword for example "few" or "other"
  72. * @return the index of the plural form corresponding to the keyword, or a negative value
  73. */
  74. static int32_t indexOrNegativeFromString(const char *keyword);
  75. /**
  76. * @param keyword for example "few" or "other"
  77. * @return the index of the plural form corresponding to the keyword, or a negative value
  78. */
  79. static int32_t indexOrNegativeFromString(const UnicodeString &keyword);
  80. /**
  81. * @param keyword for example "few" or "other"
  82. * @return the index of the plural form corresponding to the keyword, or OTHER
  83. */
  84. static int32_t indexOrOtherIndexFromString(const char *keyword) {
  85. int32_t i = indexOrNegativeFromString(keyword);
  86. return i >= 0 ? i : OTHER;
  87. }
  88. /**
  89. * @param keyword for example "few" or "other"
  90. * @return the index of the plural form corresponding to the keyword, or OTHER
  91. */
  92. static int32_t indexOrOtherIndexFromString(const UnicodeString &keyword) {
  93. int32_t i = indexOrNegativeFromString(keyword);
  94. return i >= 0 ? i : OTHER;
  95. }
  96. /**
  97. * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
  98. *
  99. * @param keyword for example "few" or "other"
  100. * @return the index of the plural form corresponding to the keyword
  101. */
  102. static int32_t indexFromString(const char *keyword, UErrorCode &errorCode);
  103. /**
  104. * Sets U_ILLEGAL_ARGUMENT_ERROR if the keyword is not a plural form.
  105. *
  106. * @param keyword for example "few" or "other"
  107. * @return the index of the plural form corresponding to the keyword
  108. */
  109. static int32_t indexFromString(const UnicodeString &keyword, UErrorCode &errorCode);
  110. };
  111. U_NAMESPACE_END
  112. #endif // !UCONFIG_NO_FORMATTING
  113. #endif // __STANDARDPLURAL_H__