~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to boost/boost/regex/v3/regex_raw_buffer.hpp

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *
 
3
 * Copyright (c) 1998-2002
 
4
 * Dr John Maddock
 
5
 *
 
6
 * Permission to use, copy, modify, distribute and sell this software
 
7
 * and its documentation for any purpose is hereby granted without fee,
 
8
 * provided that the above copyright notice appear in all copies and
 
9
 * that both that copyright notice and this permission notice appear
 
10
 * in supporting documentation.  Dr John Maddock makes no representations
 
11
 * about the suitability of this software for any purpose.
 
12
 * It is provided "as is" without express or implied warranty.
 
13
 *
 
14
 */
 
15
 
 
16
 /*
 
17
  *   LOCATION:    see http://www.boost.org for most recent version.
 
18
  *   FILE         regex_raw_buffer.hpp
 
19
  *   VERSION      see <boost/version.hpp>
 
20
  *   DESCRIPTION: Raw character buffer for regex code.
 
21
  *                Note this is an internal header file included
 
22
  *                by regex.hpp, do not include on its own.
 
23
  */
 
24
 
 
25
#ifndef BOOST_REGEX_RAW_BUFFER_HPP
 
26
#define BOOST_REGEX_RAW_BUFFER_HPP
 
27
 
 
28
#ifndef BOOST_REGEX_CONFIG_HPP
 
29
#include <boost/regex/config.hpp>
 
30
#endif
 
31
 
 
32
namespace boost{
 
33
   namespace re_detail{
 
34
 
 
35
#ifdef __BORLANDC__
 
36
   #pragma option push -a8 -b -Vx -Ve -pc
 
37
#endif
 
38
 
 
39
struct empty_padding{};
 
40
 
 
41
union padding
 
42
{
 
43
   void* p;
 
44
   unsigned int i;
 
45
};
 
46
 
 
47
template <int N>
 
48
struct padding3
 
49
{
 
50
   enum{
 
51
      padding_size = 8,
 
52
      padding_mask = 7
 
53
   };
 
54
};
 
55
 
 
56
template<>
 
57
struct padding3<2>
 
58
{
 
59
   enum{
 
60
      padding_size = 2,
 
61
      padding_mask = 1
 
62
   };
 
63
};
 
64
 
 
65
template<>
 
66
struct padding3<4>
 
67
{
 
68
   enum{
 
69
      padding_size = 4,
 
70
      padding_mask = 3
 
71
   };
 
72
};
 
73
 
 
74
template<>
 
75
struct padding3<8>
 
76
{
 
77
   enum{
 
78
      padding_size = 8,
 
79
      padding_mask = 7
 
80
   };
 
81
};
 
82
 
 
83
template<>
 
84
struct padding3<16>
 
85
{
 
86
   enum{
 
87
      padding_size = 16,
 
88
      padding_mask = 15
 
89
   };
 
90
};
 
91
 
 
92
enum{
 
93
   padding_size = padding3<sizeof(padding)>::padding_size,
 
94
   padding_mask = padding3<sizeof(padding)>::padding_mask
 
95
};
 
96
 
 
97
//
 
98
// class raw_storage
 
99
// basically this is a simplified vector<unsigned char>
 
100
// this is used by reg_expression for expression storage
 
101
//
 
102
 
 
103
template <class Allocator>
 
104
class raw_storage
 
105
{
 
106
public:
 
107
   typedef Allocator allocator_type;
 
108
   typedef typename boost::detail::rebind_allocator<unsigned char, allocator_type>::type alloc_inst_type;
 
109
   typedef typename alloc_inst_type::size_type                                size_type;
 
110
   typedef typename alloc_inst_type::pointer                                  pointer;
 
111
private:
 
112
   //
 
113
   // empty member optimisation:
 
114
   struct alloc_data : public alloc_inst_type
 
115
   {
 
116
      typename alloc_inst_type::pointer last;
 
117
      alloc_data(const Allocator& a) : alloc_inst_type(a){}
 
118
   } alloc_inst;
 
119
   pointer start, end;
 
120
public:
 
121
 
 
122
   raw_storage(const Allocator& a = Allocator());
 
123
   raw_storage(size_type n, const Allocator& a = Allocator());
 
124
 
 
125
   ~raw_storage()
 
126
   {
 
127
      alloc_inst.deallocate(start, (alloc_inst.last - start));
 
128
   }
 
129
 
 
130
   void BOOST_REGEX_CALL resize(size_type n);
 
131
   
 
132
   void* BOOST_REGEX_CALL extend(size_type n)
 
133
   {
 
134
      if(size_type(alloc_inst.last - end) < n)
 
135
         resize(n + (end - start));
 
136
      register void* result = end;
 
137
      end += n;
 
138
      return result;
 
139
   }
 
140
 
 
141
   void* BOOST_REGEX_CALL insert(size_type pos, size_type n);
 
142
 
 
143
   size_type BOOST_REGEX_CALL size()
 
144
   {
 
145
      return end - start;
 
146
   }
 
147
 
 
148
   size_type BOOST_REGEX_CALL capacity()
 
149
   {
 
150
      return alloc_inst.last - start;
 
151
   }
 
152
 
 
153
   void* BOOST_REGEX_CALL data()const
 
154
   {
 
155
      return start;
 
156
   }
 
157
 
 
158
   size_type BOOST_REGEX_CALL index(void* ptr)
 
159
   {
 
160
      return reinterpret_cast<unsigned char*>(ptr) - reinterpret_cast<unsigned char*>(data());
 
161
   }
 
162
 
 
163
   void BOOST_REGEX_CALL clear()
 
164
   {
 
165
      end = start;
 
166
   }
 
167
 
 
168
   void BOOST_REGEX_CALL align()
 
169
   {
 
170
      // move end up to a boundary:
 
171
      end = reinterpret_cast<unsigned char*>(start) + (((reinterpret_cast<unsigned char*>(end) - reinterpret_cast<unsigned char*>(start)) + padding_mask) & ~padding_mask);
 
172
   }
 
173
 
 
174
   Allocator BOOST_REGEX_CALL allocator()const;
 
175
};
 
176
 
 
177
template <class Allocator>
 
178
raw_storage<Allocator>::raw_storage(const Allocator& a)
 
179
  : alloc_inst(a)
 
180
{
 
181
  start = end = alloc_inst.allocate(1024);
 
182
  BOOST_REGEX_NOEH_ASSERT(start)
 
183
  alloc_inst.last = start + 1024;
 
184
}
 
185
 
 
186
template <class Allocator>
 
187
raw_storage<Allocator>::raw_storage(size_type n, const Allocator& a)
 
188
  : alloc_inst(a)
 
189
{
 
190
  start = end = alloc_inst.allocate(n);
 
191
  BOOST_REGEX_NOEH_ASSERT(start)
 
192
  alloc_inst.last = start + n;
 
193
}
 
194
 
 
195
template <class Allocator>
 
196
Allocator BOOST_REGEX_CALL raw_storage<Allocator>::allocator()const
 
197
{
 
198
  return alloc_inst;
 
199
}
 
200
 
 
201
template <class Allocator>
 
202
void BOOST_REGEX_CALL raw_storage<Allocator>::resize(size_type n)
 
203
{
 
204
   register size_type newsize = (alloc_inst.last - start) * 2;
 
205
   register size_type datasize = end - start;
 
206
   if(newsize < n)
 
207
      newsize = n;
 
208
   // extend newsize to WORD/DWORD boundary:
 
209
   newsize = (newsize + padding_mask) & ~(padding_mask);
 
210
 
 
211
   // allocate and copy data:
 
212
   register unsigned char* ptr = alloc_inst.allocate(newsize);
 
213
   BOOST_REGEX_NOEH_ASSERT(ptr)
 
214
   std::memcpy(ptr, start, datasize);
 
215
 
 
216
   // get rid of old buffer:
 
217
   alloc_inst.deallocate(start, (alloc_inst.last - start));
 
218
 
 
219
   // and set up pointers:
 
220
   start = ptr;
 
221
   end = ptr + datasize;
 
222
   alloc_inst.last = ptr + newsize;
 
223
}
 
224
 
 
225
template <class Allocator>
 
226
void* BOOST_REGEX_CALL raw_storage<Allocator>::insert(size_type pos, size_type n)
 
227
{
 
228
   jm_assert(pos <= size_type(end - start));
 
229
   if(size_type(alloc_inst.last - end) < n)
 
230
      resize(n + (end - start));
 
231
   register void* result = start + pos;
 
232
   std::memmove(start + pos + n, start + pos, (end - start) - pos);
 
233
   end += n;
 
234
   return result;
 
235
}
 
236
 
 
237
#ifdef __BORLANDC__
 
238
  #pragma option pop
 
239
#endif
 
240
 
 
241
} // namespace re_detail
 
242
} // namespace boost
 
243
 
 
244
#endif
 
245
 
 
246
 
 
247
 
 
248
 
 
249