]> git.donarmstrong.com Git - lilypond.git/blob - flower/stringutil.hh
728ce9bb306c93395fa0dbf6e64ee52339835079
[lilypond.git] / flower / stringutil.hh
1 #ifndef __STRING_UTIL_HH
2 #define __STRING_UTIL_HH
3 #if 0
4 #if !defined(NDEBUG)
5 #define NDEBUG BLONDE
6 #endif
7 #endif
8 const INITIALMAX=8;
9 class String_handle;
10
11
12 typedef unsigned char Byte;
13
14 #ifdef STRING_UTILS_INLINED
15 #define INLINE inline
16 #else
17 #define INLINE
18 #endif
19
20 /**Internal String struct.
21    the data itself. Handles simple tasks (resizing, resetting)
22    */
23 class StringData {
24     // GNU malloc: storage overhead is 8 bytes anyway.
25
26 friend class String_handle;
27     int maxlen; // maxlen is arraysize-1
28     
29     int length_i_;
30     Byte* data_by_p_;
31     int references;
32
33     /// init to ""
34     INLINE StringData();
35
36     /// init from src. Conservative allocation.
37     INLINE StringData(StringData const &src); 
38     
39     ~StringData();
40
41     /** POST: maxlen >= j.
42       IN: j, maximum stringlength_i_.    
43       contents thrown away.
44     */
45     INLINE void setmax(int j);
46     
47     /** POST: maxlen >= j.
48       IN: j, maximum stringlength_i_.
49       contents are kept if it grows.
50       */
51     INLINE void remax(int j);
52
53     /// check if writeable.
54     INLINE void OKW();
55
56     /// check state.
57     INLINE void OK();
58
59     /// reduce memory usage.
60     INLINE void tighten();
61
62     // assignment.
63     INLINE void set( Byte const* by_c_l, int length_i );
64
65     INLINE void set( char const* ch_c_l );
66     
67     /// concatenation.
68     INLINE void append( Byte const* by_c_l, int length_i );
69
70     INLINE void operator += ( char const* ch_c_l );
71
72     INLINE char const* ch_c_l() const; 
73
74     INLINE char* ch_l();
75
76     INLINE Byte const* by_c_l() const;
77
78     // idem, non const
79     INLINE Byte* by_l();
80
81     INLINE void trunc(int j);
82
83     /** not really safe. Can alter length_i_ without StringData knowing it.
84       */
85     INLINE Byte &operator [](int j);
86     INLINE Byte operator [](int j) const;
87 };
88
89
90
91
92 /**
93   Reference counting for strings.
94   
95    handles ref. counting, and provides a very thin interface using
96    Byte *
97
98    */
99 class String_handle {
100     StringData* data;
101     
102     /// decrease ref count. Named kind of like a Tanenbaum semafore 
103     INLINE void down();
104
105     /// increase ref count
106     INLINE void up(StringData *d);
107     
108     /** make sure data has only one reference.      
109        POST: data->references == 1
110       */
111     INLINE void copy();
112     
113 public:
114     INLINE String_handle();
115     INLINE ~String_handle();
116     INLINE String_handle(String_handle const & src);
117
118     INLINE Byte const* by_c_l() const;
119     INLINE char const* ch_c_l() const;
120     INLINE Byte* by_l();
121     INLINE char* ch_l();
122
123     INLINE void operator =(String_handle const &src);
124     INLINE void operator += (char const *s);
125     INLINE Byte operator[](int j) const;
126
127     /** Access elements. WARNING: NOT SAFE
128        don't use this for loops. Use by_c_l()
129        */
130     INLINE Byte &operator[](int j);
131     INLINE void append( Byte const* by_c_l, int length_i );
132     INLINE void set( Byte const* by_c_l, int length_i );
133     INLINE void operator = (char const *p);
134     INLINE void trunc(int j);
135     INLINE int length_i() const;
136 };
137
138 #if 0
139 #ifdef NDEBUG
140 #if (NDEBUG == BLONDE)
141 #undef NDEBUG
142 #endif
143 #endif
144 #endif
145
146 #ifdef STRING_UTILS_INLINED
147 #include "stringutil.cc"
148 #endif
149
150 #endif // __STRING_UTIL_HH //