2 String_data.inl -- implement String_data
4 source file of Flower lib
6 (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
10 #define STRINGDATA_INL
12 #include "string-data.hh"
20 const int INITIALMAX = 8;
22 #include <sys/types.h>
27 assert (ref_count_ == 1);
33 assert (maxlen >= length_);
34 assert (bool (data_byte_));
35 assert (ref_count_ >= 1);
39 String_data::String_data ()
43 data_byte_ = new Byte[maxlen + 1];
49 String_data::String_data (String_data const &src)
52 maxlen = length_ = src.length_;
53 data_byte_ = new Byte[maxlen + 1]; // should calc GNU 8byte overhead.
54 memcpy (data_byte_, src.data_byte_, length_ + 1);
58 String_data::~String_data ()
60 assert (ref_count_ == 0);
65 String_data::setmax (int j)
72 data_byte_ = new Byte[maxlen + 1];
79 /* this is all quite hairy:
86 define change authority
89 String_data::remax (int j)
94 Byte *p = new Byte[j + 1];
95 memcpy (p, data_byte_, min (maxlen, length_) + 1);
103 String_data::tighten ()
104 { // should be dec'd const
106 Byte *p = new Byte[maxlen + 1];
107 memcpy (p, data_byte_, length_ + 1);
113 String_data::set (Byte const *byte, int length_i)
117 assert (byte && byte != data_byte_);
120 remax (length_); // copies too
121 memcpy (data_byte_, byte, length_);
122 data_byte_[ length_ ] = 0;
127 String_data::set (char const *str0)
129 set ((Byte const *)str0, strlen (str0));
134 String_data::append (Byte const *byte, int length_i)
142 memcpy (data_byte_ + old_i, byte, length_i);
143 data_byte_[ length_ ] = 0;
148 String_data::operator += (char const *str0)
150 append ((Byte const *)str0, strlen (str0));
155 String_data::to_str0 () const
157 return (char const *)data_byte_;
160 String_data::get_str0 ()
162 return (char *)data_byte_;
166 String_data::to_bytes () const
172 String_data::get_bytes ()
180 String_data::trunc (int j)
183 assert (j >= 0 && j <= length_);
189 String_data::is_binary_bo () const
191 // return !memchr (data_byte_, length_, 0);
192 return ((int)strlen ((char const *)data_byte_) != length_);
196 String_data::operator [] (int j)
198 assert (j >= 0 && j <= length_);
199 return data_byte_[j];
203 String_data::operator [] (int j) const
205 assert (j >= 0 && j <= length_);
206 return data_byte_[j];
209 #endif // __STRING_UTIL_CC //