fpositer.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ******************************************************************************
  5. * Copyright (C) 2009-2012, International Business Machines Corporation and
  6. * others. All Rights Reserved.
  7. ******************************************************************************
  8. * Date Name Description
  9. * 12/14/09 doug Creation.
  10. ******************************************************************************
  11. */
  12. #include "unicode/utypes.h"
  13. #if !UCONFIG_NO_FORMATTING
  14. #include "unicode/fpositer.h"
  15. #include "cmemory.h"
  16. #include "uvectr32.h"
  17. U_NAMESPACE_BEGIN
  18. FieldPositionIterator::~FieldPositionIterator() {
  19. delete data;
  20. data = nullptr;
  21. pos = -1;
  22. }
  23. FieldPositionIterator::FieldPositionIterator()
  24. : data(nullptr), pos(-1) {
  25. }
  26. FieldPositionIterator::FieldPositionIterator(const FieldPositionIterator &rhs)
  27. : UObject(rhs), data(nullptr), pos(rhs.pos) {
  28. if (rhs.data) {
  29. UErrorCode status = U_ZERO_ERROR;
  30. data = new UVector32(status);
  31. data->assign(*rhs.data, status);
  32. if (status != U_ZERO_ERROR) {
  33. delete data;
  34. data = nullptr;
  35. pos = -1;
  36. }
  37. }
  38. }
  39. bool FieldPositionIterator::operator==(const FieldPositionIterator &rhs) const {
  40. if (&rhs == this) {
  41. return true;
  42. }
  43. if (pos != rhs.pos) {
  44. return false;
  45. }
  46. if (!data) {
  47. return rhs.data == nullptr;
  48. }
  49. return rhs.data ? data->operator==(*rhs.data) : false;
  50. }
  51. void FieldPositionIterator::setData(UVector32 *adopt, UErrorCode& status) {
  52. // Verify that adopt has valid data, and update status if it doesn't.
  53. if (U_SUCCESS(status)) {
  54. if (adopt) {
  55. if (adopt->size() == 0) {
  56. delete adopt;
  57. adopt = nullptr;
  58. } else if ((adopt->size() % 4) != 0) {
  59. status = U_ILLEGAL_ARGUMENT_ERROR;
  60. } else {
  61. for (int i = 2; i < adopt->size(); i += 4) {
  62. if (adopt->elementAti(i) >= adopt->elementAti(i+1)) {
  63. status = U_ILLEGAL_ARGUMENT_ERROR;
  64. break;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. // We own the data, even if status is in error, so we need to delete it now
  71. // if we're not keeping track of it.
  72. if (!U_SUCCESS(status)) {
  73. delete adopt;
  74. return;
  75. }
  76. delete data;
  77. data = adopt;
  78. pos = adopt == nullptr ? -1 : 0;
  79. }
  80. UBool FieldPositionIterator::next(FieldPosition& fp) {
  81. if (pos == -1) {
  82. return false;
  83. }
  84. // Ignore the first element of the tetrad: used for field category
  85. pos++;
  86. fp.setField(data->elementAti(pos++));
  87. fp.setBeginIndex(data->elementAti(pos++));
  88. fp.setEndIndex(data->elementAti(pos++));
  89. if (pos == data->size()) {
  90. pos = -1;
  91. }
  92. return true;
  93. }
  94. U_NAMESPACE_END
  95. #endif /* #if !UCONFIG_NO_FORMATTING */