Leaked source code of windows server 2003
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.

127 lines
3.7 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1993-1995
  4. * TITLE: STR.H
  5. * VERSION: 1.0
  6. * AUTHOR: jsenior
  7. * DATE: 10/28/1998
  8. *
  9. ********************************************************************************
  10. *
  11. * CHANGE LOG:
  12. *
  13. * DATE REV DESCRIPTION
  14. * ---------- ------- ----------------------------------------------------------
  15. * 10/28/1998 jsenior Original implementation.
  16. *
  17. *******************************************************************************/
  18. #ifndef _STR_H
  19. #define _STR_H
  20. #include <assert.h>
  21. template<class T>
  22. class _Str {
  23. public:
  24. _Str() : buffer(0), length(0) {}
  25. _Str(const _Str<T>& S) : buffer(0), length(0) { Copy(S); }
  26. _Str(const T* buf) : buffer(0), length(Length(buf))
  27. {
  28. if (!length) return;
  29. buffer = new T[length+1];
  30. assert(buffer);
  31. memcpy(buffer, buf, length * sizeof(T));
  32. buffer[length] = (T) 0;
  33. }
  34. ~_Str( ) { Delete(); }
  35. void Clear() { Delete(); }
  36. void erase() { Clear(); }
  37. int empty() { return length == 0; }
  38. unsigned int size() const { return length; }
  39. // equality, assignment, and append (equal)
  40. _Str<T>& operator=(const _Str<T>& rhs)
  41. { Delete(); Copy(rhs); return *this;}
  42. _Str<T> operator+(const _Str<T>& rhs)
  43. { _Str<T> s(*this); s += rhs; return s; }
  44. void operator+=(const _Str<T>& rhs) {
  45. if (!rhs.length) return;
  46. T* buf = new T[length+rhs.length+1];
  47. assert(buf);
  48. if (buffer) { memcpy(buf, buffer, length*sizeof(T)); }
  49. memcpy(buf+length, rhs.buffer, rhs.length*sizeof(T));
  50. length += rhs.length;
  51. buf[length] = (T) 0;
  52. delete[] buffer;
  53. buffer = buf;
  54. }
  55. int operator==(const _Str<T>& rhs) const {
  56. if (!buffer || !rhs.buffer) return 0;
  57. return Compare(buffer, rhs.buffer) == 0;
  58. }
  59. int operator==(const T* rhs) const {
  60. if (!buffer || !rhs) return 0;
  61. return Compare(buffer, rhs) == 0;
  62. }
  63. int operator> (const _Str<T>& rhs) const {
  64. if (!buffer || !rhs.buffer) return 0;
  65. return Compare(buffer, rhs.buffer) == 1;
  66. }
  67. int operator>=(const _Str<T>& rhs) const {
  68. if (!buffer || !rhs.buffer) return 0;
  69. return Compare(buffer, rhs.buffer) >= 0;
  70. }
  71. int operator< (const _Str<T>& rhs) const {
  72. if (!buffer || !rhs.buffer) return 0;
  73. return Compare(buffer, rhs.buffer) == -1;
  74. }
  75. int operator<=(const _Str<T>& rhs) const {
  76. if (!buffer || !rhs.buffer) return 0;
  77. return Compare(buffer, rhs.buffer) <= 0;
  78. }
  79. // casts
  80. operator const T*() const { return buffer; }
  81. const T* c_str() const { return buffer; }
  82. static unsigned int Length(const T* Buf) {
  83. if (!Buf) return 0;
  84. for (const T* t=Buf; *t; t++)
  85. ;
  86. return (unsigned int)(t - Buf);
  87. }
  88. protected:
  89. static int Compare(const T* lhs, const T*rhs) {
  90. int ret = 0;
  91. while(!(ret = (int)(*lhs - *rhs)) && *rhs)
  92. ++lhs, ++rhs;
  93. if (ret < 0) ret = -1;
  94. else if (ret > 0) ret = 1;
  95. return ret;
  96. }
  97. private:
  98. void Delete() { if (buffer) delete[] buffer; buffer = NULL; length = 0; }
  99. void Copy(const _Str<T>& S) {
  100. if (!S.length) return;
  101. buffer = new T[S.length+1];
  102. assert(buffer);
  103. length = S.length;
  104. memcpy(buffer, S.buffer, length * sizeof(T));
  105. buffer[length] = (T) 0;
  106. }
  107. protected:
  108. T* buffer;
  109. unsigned int length;
  110. };
  111. #endif _STR_H