Skip to content

Commit 7c4b2be

Browse files
authored
[libc++][NFC] Refactor basic_streambuf to use public API functions when possible (#144547)
The implementation of std::basic_streambuf used private member variables to manipulate the get and the put areas. Using public API functions is equivalent but leads to code that is easier to understand, since the public API functions are known more widely than our internal member variables. Using the public API functions removes the need to map the internal member variables back to get/put area manipulation functions in one's head. Finally, it also makes it easier to find subtle issues by instrumenting accessor functions, which is impossible if the class uses the member variables directly.
1 parent a79186c commit 7c4b2be

File tree

1 file changed

+29
-24
lines changed

1 file changed

+29
-24
lines changed

libcxx/include/streambuf

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ public:
178178
// Get and put areas:
179179
// 27.6.2.2.3 Get area:
180180
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize in_avail() {
181-
if (__ninp_ < __einp_)
182-
return static_cast<streamsize>(__einp_ - __ninp_);
181+
if (gptr() < egptr())
182+
return static_cast<streamsize>(egptr() - gptr());
183183
return showmanyc();
184184
}
185185

@@ -190,37 +190,42 @@ public:
190190
}
191191

192192
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sbumpc() {
193-
if (__ninp_ == __einp_)
193+
if (gptr() == egptr())
194194
return uflow();
195-
return traits_type::to_int_type(*__ninp_++);
195+
int_type __c = traits_type::to_int_type(*gptr());
196+
this->gbump(1);
197+
return __c;
196198
}
197199

198200
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sgetc() {
199-
if (__ninp_ == __einp_)
201+
if (gptr() == egptr())
200202
return underflow();
201-
return traits_type::to_int_type(*__ninp_);
203+
return traits_type::to_int_type(*gptr());
202204
}
203205

204206
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 streamsize sgetn(char_type* __s, streamsize __n) { return xsgetn(__s, __n); }
205207

206208
// 27.6.2.2.4 Putback:
207209
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputbackc(char_type __c) {
208-
if (__binp_ == __ninp_ || !traits_type::eq(__c, __ninp_[-1]))
210+
if (eback() == gptr() || !traits_type::eq(__c, *(gptr() - 1)))
209211
return pbackfail(traits_type::to_int_type(__c));
210-
return traits_type::to_int_type(*--__ninp_);
212+
this->gbump(-1);
213+
return traits_type::to_int_type(*gptr());
211214
}
212215

213216
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sungetc() {
214-
if (__binp_ == __ninp_)
217+
if (eback() == gptr())
215218
return pbackfail();
216-
return traits_type::to_int_type(*--__ninp_);
219+
this->gbump(-1);
220+
return traits_type::to_int_type(*gptr());
217221
}
218222

219223
// 27.6.2.2.5 Put area:
220224
inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 int_type sputc(char_type __c) {
221-
if (__nout_ == __eout_)
225+
if (pptr() == epptr())
222226
return overflow(traits_type::to_int_type(__c));
223-
*__nout_++ = __c;
227+
*pptr() = __c;
228+
this->pbump(1);
224229
return traits_type::to_int_type(__c);
225230
}
226231

@@ -312,17 +317,16 @@ protected:
312317
virtual streamsize showmanyc() { return 0; }
313318

314319
virtual streamsize xsgetn(char_type* __s, streamsize __n) {
315-
const int_type __eof = traits_type::eof();
316320
int_type __c;
317321
streamsize __i = 0;
318322
while (__i < __n) {
319-
if (__ninp_ < __einp_) {
320-
const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(__einp_ - __ninp_, __n - __i));
321-
traits_type::copy(__s, __ninp_, __len);
323+
if (gptr() < egptr()) {
324+
const streamsize __len = std::min(static_cast<streamsize>(INT_MAX), std::min(egptr() - gptr(), __n - __i));
325+
traits_type::copy(__s, gptr(), __len);
322326
__s += __len;
323327
__i += __len;
324328
this->gbump(__len);
325-
} else if ((__c = uflow()) != __eof) {
329+
} else if ((__c = uflow()) != traits_type::eof()) {
326330
*__s = traits_type::to_char_type(__c);
327331
++__s;
328332
++__i;
@@ -336,7 +340,9 @@ protected:
336340
virtual int_type uflow() {
337341
if (underflow() == traits_type::eof())
338342
return traits_type::eof();
339-
return traits_type::to_int_type(*__ninp_++);
343+
int_type __c = traits_type::to_int_type(*gptr());
344+
this->gbump(1);
345+
return __c;
340346
}
341347

342348
// 27.6.2.4.4 Putback:
@@ -345,17 +351,16 @@ protected:
345351
// 27.6.2.4.5 Put area:
346352
virtual streamsize xsputn(const char_type* __s, streamsize __n) {
347353
streamsize __i = 0;
348-
int_type __eof = traits_type::eof();
349354
while (__i < __n) {
350-
if (__nout_ >= __eout_) {
351-
if (overflow(traits_type::to_int_type(*__s)) == __eof)
355+
if (pptr() >= epptr()) {
356+
if (overflow(traits_type::to_int_type(*__s)) == traits_type::eof())
352357
break;
353358
++__s;
354359
++__i;
355360
} else {
356-
streamsize __chunk_size = std::min(__eout_ - __nout_, __n - __i);
357-
traits_type::copy(__nout_, __s, __chunk_size);
358-
__nout_ += __chunk_size;
361+
streamsize __chunk_size = std::min(epptr() - pptr(), __n - __i);
362+
traits_type::copy(pptr(), __s, __chunk_size);
363+
__pbump(__chunk_size);
359364
__s += __chunk_size;
360365
__i += __chunk_size;
361366
}

0 commit comments

Comments
 (0)