stringBuffer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _STRINGBUFFER_H_
  23. #define _STRINGBUFFER_H_
  24. //Includes
  25. #ifndef _PLATFORM_H_
  26. #include "platform/platform.h"
  27. #endif
  28. #ifndef _TVECTOR_H_
  29. #include "core/util/tVector.h"
  30. #endif
  31. #include "console/console.h"
  32. /// Utility class to wrap string manipulation in a representation
  33. /// independent way.
  34. ///
  35. /// Length does NOT include the null terminator.
  36. class StringBuffer
  37. {
  38. Vector<UTF16> mBuffer;
  39. mutable Vector<UTF8> mBuffer8;
  40. mutable bool mDirty8;
  41. public:
  42. #if defined(TORQUE_DEBUG)
  43. struct RequestCounts
  44. {
  45. U64 requestCount8;
  46. U64 requestCount16;
  47. };
  48. RequestCounts *rc;
  49. #endif
  50. StringBuffer();
  51. StringBuffer(const StringBuffer &copy);
  52. StringBuffer(const StringBuffer *in);
  53. StringBuffer(const UTF8 *in);
  54. StringBuffer(const UTF16 *in);
  55. ~StringBuffer();
  56. void append(const StringBuffer &in);
  57. void append(const UTF8* in);
  58. void append(const UTF16* in);
  59. void append(const UTF16* in, U32 len);
  60. void insert(const U32 charOffset, const StringBuffer &in);
  61. void insert(const U32 charOffset, const UTF8* in);
  62. void insert(const U32 charOffset, const UTF16* in);
  63. void insert(const U32 charOffset, const UTF16* in, const U32 len);
  64. /// Get a StringBuffer substring of length 'len' starting from 'start'.
  65. /// Returns a new StringBuffer by value;
  66. StringBuffer substring(const U32 start, const U32 len) const;
  67. /// Get a pointer to a substring of length 'len' starting from 'start'.
  68. /// Returns a raw pointer to a unicode string.
  69. /// You must delete[] the returned string when you are done with it.
  70. /// This follows the "create rule".
  71. UTF8* createSubstring8(const U32 start, const U32 len) const;
  72. UTF16* createSubstring16(const U32 start, const U32 len) const;
  73. void cut(const U32 start, const U32 len);
  74. // UTF8* cut8(const U32 start, const U32 len);
  75. // UTF16* cut16(const U32 start, const U32 len);
  76. const UTF16 getChar(const U32 offset) const;
  77. void setChar(const U32 offset, UTF16 c);
  78. void set(const StringBuffer *in);
  79. void set(const UTF8 *in);
  80. void set(const UTF16 *in);
  81. inline const U32 length() const
  82. {
  83. return mBuffer.size() - 1; // Don't count the NULL of course.
  84. }
  85. /// Get an upper bound size estimate for a UTF8 buffer to hold this
  86. /// string.
  87. const U32 getUTF8BufferSizeEstimate() const
  88. {
  89. return length() * 3 + 1;
  90. }
  91. void getCopy8(UTF8 *buff, const U32 buffSize) const;
  92. void getCopy(UTF16 *buff, const U32 buffSize) const;
  93. /// Get a copy of the contents of the string buffer.
  94. /// You must delete[] the returned copy when you are done with it.
  95. /// This follows the "create rule".
  96. UTF8* createCopy8() const;
  97. UTF16* createCopy() const;
  98. /// Get a pointer to the StringBuffer's data store.
  99. /// Use this in situations where you can be sure that the StringBuffer will
  100. /// not be modified out from under you.
  101. /// The win here is, you avoid yet another data copy. Data copy is slow on
  102. /// most modern hardware.
  103. const UTF16* getPtr() const;
  104. const UTF8* getPtr8() const;
  105. private:
  106. void updateBuffer8();
  107. #if defined(TORQUE_DEBUG)
  108. void clearRequestCounts() { rc->requestCount16 = 0; rc->requestCount8 = 0; }
  109. #endif
  110. };
  111. #endif