as_string.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. andreas@angelcode.com
  22. */
  23. // This class has been designed to be easy to use, but not necessarily efficiency.
  24. // It doesn't use shared string memory, or reference counting. It keeps track of
  25. // string length, memory size. It also makes sure that the string is null-terminated.
  26. #ifndef AS_STRING_H
  27. #define AS_STRING_H
  28. #include <stdio.h>
  29. #include <string.h>
  30. class asCString
  31. {
  32. public:
  33. asCString();
  34. ~asCString();
  35. #ifdef AS_CAN_USE_CPP11
  36. asCString(asCString &&);
  37. asCString &operator =(asCString &&);
  38. #endif // c++11
  39. asCString(const asCString &);
  40. asCString(const char *);
  41. asCString(const char *, size_t length);
  42. explicit asCString(char);
  43. void Allocate(size_t len, bool keepData);
  44. void SetLength(size_t len);
  45. size_t GetLength() const;
  46. void Concatenate(const char *str, size_t length);
  47. asCString &operator +=(const asCString &);
  48. asCString &operator +=(const char *);
  49. asCString &operator +=(char);
  50. void Assign(const char *str, size_t length);
  51. asCString &operator =(const asCString &);
  52. asCString &operator =(const char *);
  53. asCString &operator =(char);
  54. asCString SubString(size_t start, size_t length = (size_t)(-1)) const;
  55. int FindLast(const char *str, int *count = 0) const;
  56. size_t Format(const char *fmt, ...);
  57. int Compare(const char *str) const;
  58. int Compare(const asCString &str) const;
  59. int Compare(const char *str, size_t length) const;
  60. char *AddressOf();
  61. const char *AddressOf() const;
  62. char &operator [](size_t index);
  63. const char &operator[](size_t index) const;
  64. size_t RecalculateLength();
  65. protected:
  66. unsigned int length;
  67. union
  68. {
  69. char *dynamic;
  70. char local[12];
  71. };
  72. };
  73. // Helper functions
  74. bool operator ==(const asCString &, const asCString &);
  75. bool operator !=(const asCString &, const asCString &);
  76. bool operator ==(const asCString &, const char *);
  77. bool operator !=(const asCString &, const char *);
  78. bool operator ==(const char *, const asCString &);
  79. bool operator !=(const char *, const asCString &);
  80. bool operator <(const asCString &, const asCString &);
  81. asCString operator +(const asCString &, const char *);
  82. asCString operator +(const char *, const asCString &);
  83. asCString operator +(const asCString &, const asCString &);
  84. // a wrapper for using the pointer of asCString in asCMap
  85. class asCStringPointer
  86. {
  87. public:
  88. asCStringPointer();
  89. asCStringPointer(const char *str, size_t len);
  90. asCStringPointer(asCString *cstr);
  91. const char *AddressOf() const;
  92. size_t GetLength() const;
  93. bool operator==(const asCStringPointer& other) const;
  94. bool operator<(const asCStringPointer& other) const;
  95. private:
  96. // Either string/length or cstring is stored
  97. const char *string;
  98. size_t length;
  99. asCString *cstring;
  100. };
  101. #endif