summaryrefslogtreecommitdiffstats
path: root/diff_ext_for_kdiff3/diffextstring.h
blob: 132512e90d8cfae0d41d627d0b0c3d6743b2f26d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (c) 2003, Sergey Zorin. All rights reserved.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the LICENSE file provided with this software.
 *
 */

#ifndef __string_h__
#define __string_h__

#include <windows.h>
#include <tchar.h>

#include <string.h>
#include <stdlib.h>

class STRING;
inline STRING operator+( const STRING& s1, const STRING& s2);

class STRING {
  public:
    static const int begin = 0;
    static const int end = -1;
  
  public:
    STRING(const STRING& s) {
      _str = new TCHAR[s.length()+1];
      lstrcpy(_str, s);
    }
    
    STRING(const TCHAR* str = TEXT("")) {
      _str = new TCHAR[lstrlen(str)+1];
      lstrcpy(_str, str);
    }
    
    ~STRING() {
      delete[] _str;
    }

    void resize( size_t newLength )
    {
       size_t oldLength = length();
       if ( newLength < oldLength ) {
          _str[newLength] = 0; // Just truncate the string
       } else if( newLength>oldLength) {
          TCHAR* p = new TCHAR[ newLength + 1 ];
          lstrcpy(p, _str);
          for( size_t i=oldLength; i<newLength; ++i)
             p[i]=TEXT(' ');
          p[newLength]=0;
       }
    }
    
    STRING& operator=(const STRING& s) {
      delete[] _str;
      _str = new TCHAR[s.length()+1];
      lstrcpy(_str, s);
      return *this;
    }
    
    operator TCHAR*() {
      return _str;
    }
    
    operator const TCHAR*() const {
      return _str;
    }

    const TCHAR* c_str() const {
       return _str;
    }
    
    size_t length() const {
      return _tcslen(_str);
    }
    
    // Also returns the length. Behaviour like std::basic_string::size.
    // See also sizeInBytes() below.
    size_t size() const {
      return length();
    }

    // String length in bytes. May differ from length() for Unicode or MBCS
    size_t sizeInBytes() const {
      return length()*sizeof(TCHAR);
    }

    bool empty() const
    {
       return length()==0;
    }
    
    STRING substr(size_t from, size_t len=size_t(-1)) const {
      STRING tmp;
      size_t to = len==size_t(-1) ? length() : from + len;
            
      if(from < to && (to <= length())) {
        size_t new_len = to - from + 1;
        TCHAR* new_str = new TCHAR[new_len+1];
        lstrcpyn(new_str, &_str[from], int(new_len) );
        new_str[new_len] = 0;
        
        tmp = new_str;
        delete[] new_str;
      }
      
      return tmp;
    }

    STRING& replace( size_t pos, size_t num, const STRING& s )
    {
       *this = substr( 0, pos ) + s + substr( pos+num );
       return *this;
    }
    
    bool operator ==(const STRING& s) const {
      return (lstrcmp(_str, s) == 0);
    }

    size_t find(const STRING& s) const
    {
       const TCHAR* p = _tcsstr( _str, s._str );
       if (p)
          return p - _str;
       else
          return size_t(-1);
    }
    
    STRING& operator +=(const STRING& s) {
      TCHAR* str = new TCHAR[lstrlen(_str)+s.length()+1];

      lstrcpy(str, _str);
      lstrcat(str, s);
      
      delete[] _str;
      
      _str = str;
      
      return *this;
    }

  private:
    TCHAR* _str;
};

inline STRING operator+( const STRING& s1, const STRING& s2) {
  STRING tmp(s1);
  
  tmp+=s2;
  
  return tmp;
}



#endif // __string_h__