stringBuffer.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 _VECTOR_H_
  29. #include "collection/vector.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. Vector<UTF8> mBuffer8;
  40. 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. //Luma: Version of StringBuffer that doesn't convert to UTF16 for performance reasons
  56. StringBuffer(const UTF8 *in, bool bNoConvert);
  57. ~StringBuffer();
  58. void append(const StringBuffer &in);
  59. void append(const UTF8* in);
  60. void append(const UTF16* in);
  61. void append(const UTF16* in, U32 len);
  62. StringBuffer& operator=(const StringBuffer& inc);
  63. void insert(const U32 charOffset, const StringBuffer &in);
  64. void insert(const U32 charOffset, const UTF8* in);
  65. void insert(const U32 charOffset, const UTF16* in);
  66. void insert(const U32 charOffset, const UTF16* in, const U32 len);
  67. /// Get a StringBuffer substring of length 'len' starting from 'start'.
  68. /// Returns a new StringBuffer by value;
  69. StringBuffer substring(const U32 start, const U32 len) const;
  70. /// Get a pointer to a substring of length 'len' starting from 'start'.
  71. /// Returns a raw pointer to a unicode string.
  72. /// You must delete[] the returned string when you are done with it.
  73. /// This follows the "create rule".
  74. UTF8* createSubstring8(const U32 start, const U32 len) const;
  75. UTF16* createSubstring16(const U32 start, const U32 len) const;
  76. void cut(const U32 start, const U32 len);
  77. // UTF8* cut8(const U32 start, const U32 len);
  78. // UTF16* cut16(const U32 start, const U32 len);
  79. const UTF16 getChar(const U32 offset) const;
  80. void setChar(const U32 offset, UTF16 c);
  81. void set(const StringBuffer *in);
  82. void set(const UTF8 *in);
  83. void set(const UTF16 *in);
  84. //Luma: Version of StringBuffer that doesn't convert to UTF16 for performance reasons
  85. void setNoConvert(const UTF8 *in);
  86. inline const U32 length() const
  87. {
  88. return mBuffer.size() - 1; // Don't count the NULL of course.
  89. }
  90. /// Get an upper bound size estimate for a UTF8 buffer to hold this
  91. /// string.
  92. const U32 getUTF8BufferSizeEstimate() const
  93. {
  94. return length() * 3 + 1;
  95. }
  96. void getCopy8(UTF8 *buff, const U32 buffSize) const;
  97. void getCopy(UTF16 *buff, const U32 buffSize) const;
  98. /// Get a copy of the contents of the string buffer.
  99. /// You must delete[] the returned copy when you are done with it.
  100. /// This follows the "create rule".
  101. UTF8* createCopy8() const;
  102. UTF16* createCopy() const;
  103. /// Get a pointer to the StringBuffer's data store.
  104. /// Use this in situations where you can be sure that the StringBuffer will
  105. /// not be modified out from under you.
  106. /// The win here is, you avoid yet another data copy. Data copy is slow on
  107. /// most modern hardware.
  108. const UTF16* getPtr() const;
  109. // [neo, 5/7/2007]: cant be const method as it calls updateBuffer8()!
  110. //const UTF8* getPtr8() const;
  111. const UTF8* getPtr8();
  112. private:
  113. void updateBuffer8();
  114. #if defined(TORQUE_DEBUG)
  115. void clearRequestCounts() { rc->requestCount16 = 0; rc->requestCount8 = 0; }
  116. #endif
  117. };
  118. #if defined(TORQUE_DEBUG)
  119. class StringBufferManager
  120. {
  121. public:
  122. static StringBufferManager& getManager();
  123. Vector<StringBuffer*> strings;
  124. U64 request8;
  125. U64 request16;
  126. void add(StringBuffer* s);
  127. void remove(StringBuffer* s);
  128. void updateStats();
  129. void dumpStats();
  130. void dumpAllStrings();
  131. };
  132. #endif // TORQUE_DEBUG
  133. #endif