Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.1KB

  1. //-----------------------------------------------------------------------------
  2. // Project : SDK Core
  3. //
  4. // Category : Helpers
  5. // Filename : pluginterfaces/base/ustring.h
  6. // Created by : Steinberg, 12/2005
  7. // Description : UTF-16 String class
  8. //
  9. //-----------------------------------------------------------------------------
  10. // This file is part of a Steinberg SDK. It is subject to the license terms
  11. // in the LICENSE file found in the top-level directory of this distribution
  12. // and at www.steinberg.net/sdklicenses.
  13. // No part of the SDK, including this file, may be copied, modified, propagated,
  14. // or distributed except according to the terms contained in the LICENSE file.
  15. //-----------------------------------------------------------------------------
  16. #pragma once
  17. #include "ftypes.h"
  18. //------------------------------------------------------------------------
  19. namespace Steinberg {
  20. //------------------------------------------------------------------------
  21. /** UTF-16 string class without buffer management.
  22. Note: that some characters are encoded in 2 UTF16 code units (surrogate pair),
  23. this means that getLength returns the number of code unit, not the count of character! */
  24. class UString
  25. {
  26. public:
  27. //------------------------------------------------------------------------
  28. /** Construct from UTF-16 string, size is in code unit (count of char16) */
  29. UString (char16* buffer, int32 size) : thisBuffer (buffer), thisSize (size) {}
  30. /** returns buffer size */
  31. int32 getSize () const { return thisSize; }
  32. /** cast to char16* */
  33. operator const char16* () const { return thisBuffer; }
  34. /** Returns length of string (in code unit). Note this is not the count of character! */
  35. int32 getLength () const;
  36. /** Copy from UTF-16 buffer (srcSize is in code unit (count of char16)). */
  37. UString& assign (const char16* src, int32 srcSize = -1);
  38. /** Append UTF-16 buffer (srcSize is in code unit (count of char16)). */
  39. UString& append (const char16* src, int32 srcSize = -1);
  40. /** Copy to UTF-16 buffer (dstSize is in code unit (count of char16)). */
  41. const UString& copyTo (char16* dst, int32 dstSize) const;
  42. /** Copy from ASCII string (srcSize is in code unit (count of char16)). */
  43. UString& fromAscii (const char* src, int32 srcSize = -1);
  44. UString& assign (const char* src, int32 srcSize = -1) { return fromAscii (src, srcSize); }
  45. /** Copy to ASCII string. */
  46. const UString& toAscii (char* dst, int32 dstSize) const;
  47. /** Scan integer from string. */
  48. bool scanInt (int64& value) const;
  49. /** Print integer to string. */
  50. bool printInt (int64 value);
  51. /** Scan float from string. */
  52. bool scanFloat (double& value) const;
  53. /** Print float to string. */
  54. bool printFloat (double value, int32 precision = 4);
  55. //------------------------------------------------------------------------
  56. protected:
  57. char16* thisBuffer;
  58. int32 thisSize; ///< size in code unit (not in byte!)
  59. };
  60. //------------------------------------------------------------------------
  61. /** UTF-16 string with fixed buffer size.
  62. */
  63. template <int32 maxSize>
  64. class UStringBuffer : public UString
  65. {
  66. public:
  67. //------------------------------------------------------------------------
  68. UStringBuffer () : UString (data, maxSize) { data[0] = 0; }
  69. /** Construct from UTF-16 string. */
  70. UStringBuffer (const char16* src, int32 srcSize = -1) : UString (data, maxSize)
  71. {
  72. data[0] = 0;
  73. if (src)
  74. assign (src, srcSize);
  75. }
  76. /** Construct from ASCII string. */
  77. UStringBuffer (const char* src, int32 srcSize = -1) : UString (data, maxSize)
  78. {
  79. data[0] = 0;
  80. if (src)
  81. fromAscii (src, srcSize);
  82. }
  83. //------------------------------------------------------------------------
  84. protected:
  85. char16 data[maxSize];
  86. };
  87. //------------------------------------------------------------------------
  88. typedef UStringBuffer<128> UString128; ///< 128 character UTF-16 string
  89. typedef UStringBuffer<256> UString256; ///< 256 character UTF-16 string
  90. } // namespace Steinberg
  91. //------------------------------------------------------------------------
  92. #define USTRING(asciiString) Steinberg::UString256 (asciiString)
  93. #define USTRINGSIZE(var) (sizeof (var) / sizeof (Steinberg::char16))
  94. //------------------------------------------------------------------------