format.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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-2012, International Business Machines Corporation and *
  6. * others. All Rights Reserved. *
  7. *******************************************************************************
  8. *
  9. * File FORMAT.CPP
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/19/97 aliu Converted from java.
  15. * 03/17/97 clhuang Implemented with new APIs.
  16. * 03/27/97 helena Updated to pass the simple test after code review.
  17. * 07/20/98 stephen Added explicit init values for Field/ParsePosition
  18. ********************************************************************************
  19. */
  20. // *****************************************************************************
  21. // This file was generated from the java source file Format.java
  22. // *****************************************************************************
  23. #include "utypeinfo.h" // for 'typeid' to work
  24. #include "unicode/utypes.h"
  25. #ifndef U_I18N_IMPLEMENTATION
  26. #error U_I18N_IMPLEMENTATION not set - must be set for all ICU source files in i18n/ - see https://unicode-org.github.io/icu/userguide/howtouseicu
  27. #endif
  28. /*
  29. * Dummy code:
  30. * If all modules in the I18N library are switched off, then there are no
  31. * library exports and MSVC 6 writes a .dll but not a .lib file.
  32. * Unless we export _something_ in that case...
  33. */
  34. #if UCONFIG_NO_COLLATION && UCONFIG_NO_FORMATTING && UCONFIG_NO_TRANSLITERATION
  35. U_CAPI int32_t U_EXPORT2
  36. uprv_icuin_lib_dummy(int32_t i) {
  37. return -i;
  38. }
  39. #endif
  40. /* Format class implementation ---------------------------------------------- */
  41. #if !UCONFIG_NO_FORMATTING
  42. #include "unicode/format.h"
  43. #include "unicode/ures.h"
  44. #include "cstring.h"
  45. #include "locbased.h"
  46. // *****************************************************************************
  47. // class Format
  48. // *****************************************************************************
  49. U_NAMESPACE_BEGIN
  50. UOBJECT_DEFINE_RTTI_IMPLEMENTATION(FieldPosition)
  51. FieldPosition::~FieldPosition() {}
  52. FieldPosition *
  53. FieldPosition::clone() const {
  54. return new FieldPosition(*this);
  55. }
  56. // -------------------------------------
  57. // default constructor
  58. Format::Format()
  59. : UObject()
  60. {
  61. *validLocale = *actualLocale = 0;
  62. }
  63. // -------------------------------------
  64. Format::~Format()
  65. {
  66. }
  67. // -------------------------------------
  68. // copy constructor
  69. Format::Format(const Format &that)
  70. : UObject(that)
  71. {
  72. *this = that;
  73. }
  74. // -------------------------------------
  75. // assignment operator
  76. Format&
  77. Format::operator=(const Format& that)
  78. {
  79. if (this != &that) {
  80. uprv_strcpy(validLocale, that.validLocale);
  81. uprv_strcpy(actualLocale, that.actualLocale);
  82. }
  83. return *this;
  84. }
  85. // -------------------------------------
  86. // Formats the obj and append the result in the buffer, toAppendTo.
  87. // This calls the actual implementation in the concrete subclasses.
  88. UnicodeString&
  89. Format::format(const Formattable& obj,
  90. UnicodeString& toAppendTo,
  91. UErrorCode& status) const
  92. {
  93. if (U_FAILURE(status)) return toAppendTo;
  94. FieldPosition pos(FieldPosition::DONT_CARE);
  95. return format(obj, toAppendTo, pos, status);
  96. }
  97. // -------------------------------------
  98. // Default implementation sets unsupported error; subclasses should
  99. // override.
  100. UnicodeString&
  101. Format::format(const Formattable& /* unused obj */,
  102. UnicodeString& toAppendTo,
  103. FieldPositionIterator* /* unused posIter */,
  104. UErrorCode& status) const
  105. {
  106. if (!U_FAILURE(status)) {
  107. status = U_UNSUPPORTED_ERROR;
  108. }
  109. return toAppendTo;
  110. }
  111. // -------------------------------------
  112. // Parses the source string and create the corresponding
  113. // result object. Checks the parse position for errors.
  114. void
  115. Format::parseObject(const UnicodeString& source,
  116. Formattable& result,
  117. UErrorCode& status) const
  118. {
  119. if (U_FAILURE(status)) return;
  120. ParsePosition parsePosition(0);
  121. parseObject(source, result, parsePosition);
  122. if (parsePosition.getIndex() == 0) {
  123. status = U_INVALID_FORMAT_ERROR;
  124. }
  125. }
  126. // -------------------------------------
  127. bool
  128. Format::operator==(const Format& that) const
  129. {
  130. // Subclasses: Call this method and then add more specific checks.
  131. return typeid(*this) == typeid(that);
  132. }
  133. //---------------------------------------
  134. /**
  135. * Simple function for initializing a UParseError from a UnicodeString.
  136. *
  137. * @param pattern The pattern to copy into the parseError
  138. * @param pos The position in pattern where the error occurred
  139. * @param parseError The UParseError object to fill in
  140. * @draft ICU 2.4
  141. */
  142. void Format::syntaxError(const UnicodeString& pattern,
  143. int32_t pos,
  144. UParseError& parseError) {
  145. parseError.offset = pos;
  146. parseError.line=0; // we are not using line number
  147. // for pre-context
  148. int32_t start = (pos < U_PARSE_CONTEXT_LEN)? 0 : (pos - (U_PARSE_CONTEXT_LEN-1
  149. /* subtract 1 so that we have room for null*/));
  150. int32_t stop = pos;
  151. pattern.extract(start,stop-start,parseError.preContext,0);
  152. //null terminate the buffer
  153. parseError.preContext[stop-start] = 0;
  154. //for post-context
  155. start = pos+1;
  156. stop = ((pos+U_PARSE_CONTEXT_LEN)<=pattern.length()) ? (pos+(U_PARSE_CONTEXT_LEN-1)) :
  157. pattern.length();
  158. pattern.extract(start,stop-start,parseError.postContext,0);
  159. //null terminate the buffer
  160. parseError.postContext[stop-start]= 0;
  161. }
  162. Locale
  163. Format::getLocale(ULocDataLocaleType type, UErrorCode& status) const {
  164. U_LOCALE_BASED(locBased, *this);
  165. return locBased.getLocale(type, status);
  166. }
  167. const char *
  168. Format::getLocaleID(ULocDataLocaleType type, UErrorCode& status) const {
  169. U_LOCALE_BASED(locBased, *this);
  170. return locBased.getLocaleID(type, status);
  171. }
  172. void
  173. Format::setLocaleIDs(const char* valid, const char* actual) {
  174. U_LOCALE_BASED(locBased, *this);
  175. locBased.setLocaleIDs(valid, actual);
  176. }
  177. U_NAMESPACE_END
  178. #endif /* #if !UCONFIG_NO_FORMATTING */
  179. //eof