stringpiece.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // Copyright (C) 2009-2013, International Business Machines
  4. // Corporation and others. All Rights Reserved.
  5. //
  6. // Copyright 2004 and onwards Google Inc.
  7. //
  8. // Author: wilsonh@google.com (Wilson Hsieh)
  9. //
  10. #include "unicode/utypes.h"
  11. #include "unicode/stringpiece.h"
  12. #include "cstring.h"
  13. #include "cmemory.h"
  14. U_NAMESPACE_BEGIN
  15. StringPiece::StringPiece(const char* str)
  16. : ptr_(str), length_((str == nullptr) ? 0 : static_cast<int32_t>(uprv_strlen(str))) { }
  17. StringPiece::StringPiece(const StringPiece& x, int32_t pos) {
  18. if (pos < 0) {
  19. pos = 0;
  20. } else if (pos > x.length_) {
  21. pos = x.length_;
  22. }
  23. ptr_ = x.ptr_ + pos;
  24. length_ = x.length_ - pos;
  25. }
  26. StringPiece::StringPiece(const StringPiece& x, int32_t pos, int32_t len) {
  27. if (pos < 0) {
  28. pos = 0;
  29. } else if (pos > x.length_) {
  30. pos = x.length_;
  31. }
  32. if (len < 0) {
  33. len = 0;
  34. } else if (len > x.length_ - pos) {
  35. len = x.length_ - pos;
  36. }
  37. ptr_ = x.ptr_ + pos;
  38. length_ = len;
  39. }
  40. void StringPiece::set(const char* str) {
  41. ptr_ = str;
  42. if (str != nullptr)
  43. length_ = static_cast<int32_t>(uprv_strlen(str));
  44. else
  45. length_ = 0;
  46. }
  47. int32_t StringPiece::find(StringPiece needle, int32_t offset) {
  48. if (length() == 0 && needle.length() == 0) {
  49. return 0;
  50. }
  51. // TODO: Improve to be better than O(N^2)?
  52. for (int32_t i = offset; i < length(); i++) {
  53. int32_t j = 0;
  54. for (; j < needle.length(); i++, j++) {
  55. if (data()[i] != needle.data()[j]) {
  56. i -= j;
  57. goto outer_end;
  58. }
  59. }
  60. return i - j;
  61. outer_end: void();
  62. }
  63. return -1;
  64. }
  65. int32_t StringPiece::compare(StringPiece other) {
  66. int32_t i = 0;
  67. for (; i < length(); i++) {
  68. if (i == other.length()) {
  69. // this is longer
  70. return 1;
  71. }
  72. char a = data()[i];
  73. char b = other.data()[i];
  74. if (a < b) {
  75. return -1;
  76. } else if (a > b) {
  77. return 1;
  78. }
  79. }
  80. if (i < other.length()) {
  81. // other is longer
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. U_EXPORT UBool U_EXPORT2
  87. operator==(const StringPiece& x, const StringPiece& y) {
  88. int32_t len = x.size();
  89. if (len != y.size()) {
  90. return false;
  91. }
  92. if (len == 0) {
  93. return true;
  94. }
  95. const char* p = x.data();
  96. const char* p2 = y.data();
  97. // Test last byte in case strings share large common prefix
  98. --len;
  99. if (p[len] != p2[len]) return false;
  100. // At this point we can, but don't have to, ignore the last byte.
  101. return uprv_memcmp(p, p2, len) == 0;
  102. }
  103. const int32_t StringPiece::npos = 0x7fffffff;
  104. U_NAMESPACE_END