12 #ifndef __UT_StringStreamImpl__
13 #define __UT_StringStreamImpl__
15 #ifndef __UT_StringStream__
16 #error UT_StringStreamImpl.h should only be included by UT_StringStream.h
25 std::ios_base::openmode
mode,
28 : myGPos(-1), myPPos(-1)
44 if(mode & std::ios_base::in)
45 setg64(start, start, start +
length());
47 setg(
nullptr,
nullptr,
nullptr);
49 if(mode & std::ios_base::out)
51 setp64(start, start, start +
length());
55 setp(
nullptr,
nullptr);
92 s = std::move(myBuffer);
96 inline std::streambuf::pos_type
97 UT_StringStreamBuffer::seekoff(
98 std::streambuf::off_type
offset,
99 std::ios_base::seekdir dir,
100 std::ios_base::openmode
mode)
103 if((mode & std::ios_base::in) && gptr())
106 if(dir == std::ios_base::beg)
108 else if(dir == std::ios_base::cur)
109 pos = ((myGPos < 0) ? (gptr() -
buffer()) : myGPos) +
offset;
120 if((mode & std::ios_base::out) && pptr())
123 if(dir == std::ios_base::beg)
125 else if(dir == std::ios_base::cur)
126 pos = ((myPPos < 0) ? (pptr() -
buffer()) : myPPos) +
offset;
140 inline std::streambuf::pos_type
141 UT_StringStreamBuffer::seekpos(
142 std::streambuf::pos_type pos,
143 std::ios_base::openmode mode)
148 if((mode & std::ios_base::in) && gptr())
153 if((mode & std::ios_base::out) && pptr())
162 inline std::streambuf::int_type
163 UT_StringStreamBuffer::underflow()
165 const char *cur = gptr();
168 updategpos((myGPos < 0) ? (cur -
buffer()) : myGPos);
173 return traits_type::eof();
176 inline std::streamsize
177 UT_StringStreamBuffer::xsgetn(
178 std::streambuf::char_type *
dst,
181 const char *cur = gptr();
183 return traits_type::eof();
194 ::memcpy(dst, start + pos, num);
195 updategpos(pos + num);
202 inline std::streamsize
203 UT_StringStreamBuffer::showmanyc()
205 const char *cur = gptr();
207 return traits_type::eof();
210 return egptr() - cur;
214 inline std::streambuf::int_type
215 UT_StringStreamBuffer::pbackfail(std::streambuf::int_type
c)
217 const char *cur = gptr();
220 const char *start =
buffer();
221 exint pos = ((myGPos < 0) ? (cur - start) : myGPos) - 1;
222 if(pos >= 0 && pos <
length() && start[pos] ==
c)
228 return traits_type::eof();
231 inline std::streambuf::int_type
232 UT_StringStreamBuffer::overflow(int_type c)
234 if(c != traits_type::eof())
237 if(xsputn(&ch, 1) > 0)
240 return traits_type::eof();
243 inline std::streamsize
244 UT_StringStreamBuffer::xsputn(
245 const std::streambuf::char_type *
src,
248 const char *old_start =
buffer();
249 const char *cur_g = gptr();
250 exint pos = (myPPos < 0) ? (pptr() - old_start) : myPPos;
260 char *
buf = myBuffer.
lock(0, end);
262 ::memset(buf + old_len, 0, pos - old_len);
263 ::memcpy(buf + pos, src, num);
265 if(cur_g && end > old_len)
266 updategpos((myGPos < 0) ? (cur_g - old_start) : myGPos);
267 updateppos(pos + num);
272 UT_StringStreamBuffer::init()
274 char *start =
const_cast<char*
>(
buffer());
275 char *end = start +
length();
279 setg64(start, start, end);
284 setp64(start, end, end);
293 utClampBuf(
char *&start,
char *&cur,
char *&end)
296 if(end - start <= size_limit)
301 const exint backup_limit = 1024;
302 if(cur - start > backup_limit)
304 start = cur - backup_limit;
305 if(end - start <= size_limit)
308 end = start + size_limit;
313 UT_StringStreamBuffer::setg64(
char *start,
char *cur,
char *end)
315 UT_ASSERT(start && cur && end && start <= cur && cur <= end);
316 utClampBuf(start, cur, end);
317 setg(start, cur, end);
321 UT_StringStreamBuffer::setp64(
char *start,
char *cur,
char *end)
323 UT_ASSERT(start && cur && end && start <= cur && cur <= end);
324 utClampBuf(start, cur, end);
337 UT_StringStreamBuffer::updategpos(
exint pos)
340 char *start =
const_cast<char*
>(
buffer());
344 setg64(start, start, start);
349 setg64(start, start + pos, start +
length());
354 UT_StringStreamBuffer::updateppos(
exint pos)
357 char *start =
const_cast<char*
>(
buffer());
361 setp64(start, start, start);
366 setp64(start, start + pos, start +
length());
375 utBumpAlloc(
exint current_size)
377 static exint big_alloc_threshold = 1024 * 1024;
386 if (current_size < 4096)
392 if (current_size < big_alloc_threshold)
393 bump = current_size / 8;
394 else if (current_size < big_alloc_threshold * 8)
395 bump = current_size / 4;
397 bump = current_size / 2;
399 current_size += bump;
410 const char *old_start =
buffer();
411 const char *cur_g = gptr();
412 const char *cur_p = pptr();
415 updategpos((myGPos < 0) ? (cur_g - old_start) : myGPos);
417 updateppos((myPPos < 0) ? (cur_p - old_start) : myPPos);
422 UT_StringStreamBuffer::updateBufferEnd()
435 #endif // UT_StringStreamImpl
void stealIntoStringHolder(UT_StringHolder &s)
void reserve(exint bytes=0)
GLenum GLuint GLenum GLsizei const GLchar * buf
const char * buffer() const
SYS_FORCE_INLINE exint getAllocatedSize() const
GLboolean GLboolean GLboolean GLboolean a
SYS_FORCE_INLINE void strcpy(const char *src)
**But if you need a result
void releaseSetLength(exint new_length)
void swap(UT_WorkBuffer &other)
void stealIntoString(UT_String &str)
#define UT_INITIAL_BUFFER_SIZE
UT_StringStreamBuffer(std::ios_base::openmode mode=(std::ios_base::in|std::ios_base::out), const char *buf=nullptr, exint len=-1)
char * lock(exint offset=0, exint reserve_bytes=0)
void swap(UT_WorkBuffer &buf)
const UT_WorkBuffer & str()
SYS_FORCE_INLINE void append(char character)
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
SYS_FORCE_INLINE void clear()
void stealIntoString(UT_String &s)