aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/stllike/asciistream.cpp
blob: bccd0fa744ed96bea151ff19f95ce4bd99016bb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "asciistream.h"
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/util/exceptions.h>
#include <vespa/vespalib/util/size_literals.h>
#include <vespa/vespalib/util/alloc.h>
#include <vespa/vespalib/locale/c.h>
#include <vespa/fastos/file.h>
#include <limits>
#include <cassert>
#include <charconv>
#include <vector>

#include <vespa/log/log.h>
LOG_SETUP(".vespalib.stllike.asciistream");

namespace vespalib {

namespace {

std::vector<string>
getPrecisions(const char type) {
    std::vector<string> result(VESPALIB_ASCIISTREAM_MAX_PRECISION + 1);
    for (uint32_t i=0; i<result.size(); ++i) {
        char buf[8];
        int count = snprintf(buf, sizeof(buf), "%%.%u%c", i, type);
        assert(size_t(count) < sizeof(buf));  // Assert no truncation.
        (void) count;
        result[i] = buf;
    }
    return result;
}
std::vector<string> fixedPrecisions = getPrecisions('f');
std::vector<string> scientificPrecisions = getPrecisions('e');
std::vector<string> autoPrecisions = getPrecisions('g');

}

asciistream &
asciistream::operator << (Precision v) {
    assert(v.getPrecision() <= VESPALIB_ASCIISTREAM_MAX_PRECISION);
    _precision = v.getPrecision();
    return *this;
}

asciistream &
asciistream::operator >> (Precision v) {
    assert(v.getPrecision() <= VESPALIB_ASCIISTREAM_MAX_PRECISION);
    _precision = v.getPrecision();
    return *this;
}

asciistream::asciistream() :
    _rPos(0),
    _wbuf(),
    _rbuf(_wbuf.c_str(), _wbuf.size()),
    _base(dec),
    _floatSpec(automatic),
    _floatModifier(defaultdotting),
    _width(0),
    _fill(' '),
    _precision(6)
{ }

asciistream::asciistream(stringref buf) :
    _rPos(0),
    _wbuf(),
    _rbuf(buf),
    _base(dec),
    _floatSpec(automatic),
    _floatModifier(defaultdotting),
    _width(0),
    _fill(' '),
    _precision(6)
{
    if (buf[buf.size()] != '\0') {
        _wbuf = buf;
        _rbuf = _wbuf;
    }
}

asciistream::~asciistream() = default;

asciistream::asciistream(const asciistream & rhs) :
    _rPos(0),
    _wbuf(rhs.str()),
    _rbuf(_wbuf.c_str(), _wbuf.size()),
    _base(rhs._base),
    _floatSpec(rhs._floatSpec),
    _floatModifier(rhs._floatModifier),
    _width(rhs._width),
    _fill(rhs._fill),
    _precision(rhs._precision)
{
}

asciistream &
asciistream::operator = (const asciistream & rhs)
{
    if (this != &rhs) {
        asciistream newStream(rhs);
        swap(newStream);
    }
    return *this;
}

asciistream::asciistream(asciistream && rhs) noexcept
    : asciistream()
{
    swap(rhs);
}

asciistream &
asciistream::operator = (asciistream && rhs) noexcept
{
    if (this != &rhs) {
        swap(rhs);
    }
    return *this;
}

void
asciistream::swap(asciistream & rhs) noexcept
{
    std::swap(_rPos, rhs._rPos);
    // If read-only, _wbuf is empty and _rbuf is set
    // If ever written to, _rbuf == _wbuf
    const bool lhs_read_only = (_rbuf.data() != _wbuf.data());
    const bool rhs_read_only = (rhs._rbuf.data() != rhs._wbuf.data());
    std::swap(_wbuf, rhs._wbuf);
    std::swap(_rbuf, rhs._rbuf);
    if (!lhs_read_only) {
        rhs._rbuf = rhs._wbuf;
    }
    if (!rhs_read_only) {
        _rbuf = _wbuf;
    }

    std::swap(_base, rhs._base);
    std::swap(_floatSpec, rhs._floatSpec);
    std::swap(_floatModifier, rhs._floatModifier);
    std::swap(_width, rhs._width);
    std::swap(_precision, rhs._precision);
    std::swap(_fill, rhs._fill);
}

namespace {

int getValue(double & val, const char *buf) __attribute__((noinline));
int getValue(float & val, const char *buf) __attribute__((noinline));
void throwInputError(int e, const char * t, const char * buf) __attribute__((noinline));
void throwInputError(std::errc e, const char * t, const char * buf) __attribute__((noinline));
void throwUnderflow(size_t pos) __attribute__((noinline));
template <typename T>
T strToInt(T & v, const char *begin, const char *end) __attribute__((noinline));

void
throwInputError(int e, const char * t, const char * buf)
{
    if (e == 0) {
        throw IllegalArgumentException("Failed decoding a " + string(t) + " from '" + string(buf) + "'.", VESPA_STRLOC);
    } else if (errno == ERANGE) {
        throw IllegalArgumentException(string(t) + " value '" + string(buf) + "' is outside of range.", VESPA_STRLOC);
    } else if (errno == EINVAL) {
        throw IllegalArgumentException("Illegal " + string(t) + " value '" + string(buf) + "'.", VESPA_STRLOC);
    } else {
        throw IllegalArgumentException("Unknown error decoding an " + string(t) + " from '" + string(buf) + "'.", VESPA_STRLOC);
    }
}

void
throwInputError(std::errc e, const char * t, const char * buf) {
    if (e == std::errc::invalid_argument) {
        throw IllegalArgumentException("Illegal " + string(t) + " value '" + string(buf) + "'.", VESPA_STRLOC);
    } else if (e == std::errc::result_out_of_range) {
        throw IllegalArgumentException(string(t) + " value '" + string(buf) + "' is outside of range.", VESPA_STRLOC);
    } else {
        throw IllegalArgumentException("Unknown error decoding an " + string(t) + " from '" + string(buf) + "'.", VESPA_STRLOC);
    }
}

void
throwUnderflow(size_t pos)
{
    throw IllegalArgumentException(make_string("buffer underflow at pos %ld.", pos), VESPA_STRLOC);
}

int
getValue(double & val, const char *buf)
{
    char *ebuf;
    errno = 0;
    val = locale::c::strtod_au(buf, &ebuf);
    if ((errno != 0) || (buf == ebuf)) {
        throwInputError(errno, "double", buf);
    }
    return ebuf - buf;
}

int
getValue(float & val, const char *buf)
{
    char *ebuf;
    errno = 0;
    val = locale::c::strtof_au(buf, &ebuf);
    if ((errno != 0) || (buf == ebuf)) {
        throwInputError(errno, "float", buf);
    }
    return ebuf - buf;
}

template <typename T>
T
strToInt(T & v, const char *begin, const char *end)
{
    const char * curr = begin;
    for (;(curr < end) && std::isspace(*curr); curr++);

    std::from_chars_result err;
    if (((end - curr) > 2) && (curr[0] == '0') && ((curr[1] | 0x20) == 'x')) {
        err = std::from_chars(curr+2, end, v, 16);
    } else {
        err = std::from_chars(curr, end, v, 10);
    }
    if (err.ec == std::errc::invalid_argument) {
        if (err.ptr >= end) {
            throwUnderflow(err.ptr - begin);
        }
        throwInputError(err.ec, "strToInt", begin);
    } else if (err.ec == std::errc::result_out_of_range) {
        throwInputError(err.ec, "strToInt", begin);
    }

    return err.ptr - begin;
}

}

asciistream &
asciistream::operator >> (bool & v)
{
    for (;(_rPos < length()) && std::isspace(_rbuf[_rPos]); _rPos++);
    if (_rPos < length()) {
        v = (_rbuf[_rPos++] != '0');
    } else {
        throwUnderflow(_rPos);
    }
    return *this;
}

asciistream &
asciistream::operator >> (char & v)
{
    for (;(_rPos < length()) && std::isspace(_rbuf[_rPos]); _rPos++);
    if (_rPos < length()) {
        v = _rbuf[_rPos++];
    } else {
        throwUnderflow(_rPos);
    }
    return *this;
}

asciistream &
asciistream::operator >> (signed char & v)
{
    for (;(_rPos < length()) && std::isspace(_rbuf[_rPos]); _rPos++);
    if (_rPos < length()) {
        v = _rbuf[_rPos++];
    } else {
        throwUnderflow(_rPos);
    }
    return *this;
}

asciistream &
asciistream::operator >> (unsigned char & v)
{
    for (;(_rPos < length()) && std::isspace(_rbuf[_rPos]); _rPos++);
    if (_rPos < length()) {
        v = _rbuf[_rPos++];
    } else {
        throwUnderflow(_rPos);
    }
    return *this;
}

asciistream &
asciistream::operator >> (unsigned short & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (unsigned int & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (unsigned long & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (unsigned long long & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (short & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (int & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (long & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (long long & v)
{
    _rPos += strToInt(v, &_rbuf[_rPos], &_rbuf[length()]);
    return *this;
}

asciistream &
asciistream::operator >> (double & v)
{
    double l(0);
    _rPos += getValue(l, &_rbuf[_rPos]);
    v = l;
    return *this;
}

asciistream &
asciistream::operator >> (float & v)
{
    float l(0);
    _rPos += getValue(l, &_rbuf[_rPos]);
    v = l;
    return *this;
}

void
asciistream::eatWhite()
{
    for (;(_rPos < length()) && isspace(_rbuf[_rPos]); _rPos++);
}

void asciistream::eatNonWhite()
{
    for (;(_rPos < length()) && !isspace(_rbuf[_rPos]); _rPos++);
}

asciistream &
asciistream::operator >> (std::string & v)
{
    eatWhite();
    size_t start(_rPos);
    eatNonWhite();
    v.assign(&_rbuf[start], _rPos-start);
    return *this;
}

asciistream &
asciistream::operator >> (string & v)
{
    eatWhite();
    size_t start(_rPos);
    eatNonWhite();
    v.assign(&_rbuf[start], _rPos-start);
    return *this;
}


namespace {
const char * _C_char = "0123456789abcdefg";

char *
prependInt(char * tmp, Base base)
{
    if (base == bin) {
        tmp[1] = 'b';
        tmp[0] = '0';
        return tmp;
    }
    return tmp + 2;
}

char *
prependSign(bool sign, char * tmp)
{
    if (sign) {
        tmp[0] = '-';
        return tmp;
    }
    return tmp + 1;
}

template <uint8_t base>
uint8_t printInt(unsigned long long r, char * tmp, uint8_t i) __attribute__((noinline));

template <uint8_t base>
uint8_t
printInt(unsigned long long r, char * tmp, uint8_t i)
{
    for(; r; i--, r/=base) {
        uint8_t d = r%base;
        tmp[i-1] = (base <= 10) ? d + '0' : _C_char[d];
    }
    return i;
}

unsigned long long normalize(long long v, bool &negative) {
    if (v < 0) {
        negative = true;
        if (v == std::numeric_limits<long long>::min()) {
            // according to UBSAN:
            // negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself
            return v;
        }
        return -v;
    }
    return v;
}

}


asciistream &
asciistream::operator << (long long v_in)
{
    char tmp[72];
    uint8_t i(sizeof(tmp));
    bool negative(false);
    if (v_in == 0) {
        tmp[--i] = '0';
    } else {
        unsigned long long v = normalize(v_in, negative);
        switch (_base) {
          case 2:
            i = printInt<2>(v, tmp, i); break;
          case 8:
            i = printInt<8>(v, tmp, i); break;
          case 10:
            i = printInt<10>(v, tmp, i); break;
          case 16:
            i = printInt<16>(v, tmp, i); break;
          default:
            assert(!"unhandled number base");
        }
    }
    const char *final = prependSign(negative, prependInt(tmp+i-2, _base)-1);
    doFill(sizeof(tmp)-(final-tmp));
    write(final, sizeof(tmp)-(final-tmp));
    return *this;
}

void
asciistream::doReallyFill(size_t currWidth)
{
    for (; _width > currWidth; currWidth++) {
        write(&_fill, 1);
    }
}

asciistream &
asciistream::operator << (unsigned long long v)
{
    char tmp[72];
    uint8_t i(sizeof(tmp));
    if (v == 0) {
        tmp[--i] = '0';
    } else {
        switch (_base) {
          case 2:
            i = printInt<2>(v, tmp, i); break;
          case 8:
            i = printInt<8>(v, tmp, i); break;
          case 10:
            i = printInt<10>(v, tmp, i); break;
          case 16:
            i = printInt<16>(v, tmp, i); break;
          default:
            assert(!"unhandled number base");
        }
    }
    const char *final = prependInt(tmp+i-2, _base);
    doFill(sizeof(tmp)-(final-tmp));
    write(final, sizeof(tmp)-(final-tmp));
    return *this;
}

namespace {
struct BaseStateSaver {
    asciistream& _stream;
    Base _savedBase;
    BaseStateSaver(asciistream& stream, Base base)
        : _stream(stream), _savedBase(base) {}
    ~BaseStateSaver() {
        _stream << Base(_savedBase);
    }
};
}

asciistream &
asciistream::operator<<(const void* p)
{
    BaseStateSaver saver(*this, _base);
    return *this << "0x" << hex << reinterpret_cast<uint64_t>(p);
}

asciistream &
asciistream::operator << (float v)
{
    if (_floatSpec == fixed) {
        printFixed(v);
    } else {
        printScientific(v);
    }
    return *this;
}

asciistream &
asciistream::operator << (double v)
{
    if (_floatSpec == fixed) {
        printFixed(v);
    } else {
        printScientific(v);
    }
    return *this;
}

template <typename T>
void asciistream::printFixed(T v)
{
    char tmp[sizeof(T)*64]; // Double::max printed fixed takes 316 bytes with default
                  // precision, a high precision adds even more.
    const char *spec = fixedPrecisions[_precision].c_str();
    int len = snprintf(tmp, sizeof(tmp), spec, v);
    assert(len < static_cast<int>(sizeof(tmp)));
    doFill(len);
    write(tmp, len);
}

namespace {
bool hasDotOrIsScientific(const char* string, size_t len) {
    for (size_t i=0; i<len; ++i) {
        switch (string[i]) {
            case '.':
            case ',':
            case 'e':
            case 'E':
                return true;
            default:
                break;
        }
    }
    return false;
}
}

template <typename T>
void asciistream::printScientific(T v)
{
    char tmp[sizeof(T)*8];
    const char *spec = (((_floatSpec == scientific)
                ? scientificPrecisions[_precision]
                : autoPrecisions[_precision])).c_str();
    int len = snprintf(tmp, sizeof(tmp), spec, v);
    assert(len < static_cast<int>(sizeof(tmp)));
    doFill(len);
    write(tmp, len);
    if (_floatModifier == forcedot && !hasDotOrIsScientific(tmp, len)) {
        write(".0", 2);
    }
}

void
asciistream::write(const void * buf, size_t len)
{
    if (_rPos > 0 && _rPos == length()) {
        clear();
    }
    if (_rbuf.data() != _wbuf.data()) {
        if (_wbuf.empty()) {
            _wbuf = _rbuf; // Read only to RW
        } else {
            LOG_ABORT("should not be reached");  // Impossible
        }
    }
    _wbuf.append(buf, len);
    _rbuf = _wbuf;
}

string
asciistream::getline(char delim)
{
    string line;
    const size_t start(_rPos);
    const size_t end(_rbuf.size());
    for (; (_rPos < end) && (_rbuf[_rPos] != delim); _rPos++);
    if (_rPos > start) {
        line.assign(&_rbuf[start], _rPos - start);
    }
    if (_rPos < end) {
        _rPos++;  // eat the terminating\n
    }
    return line;
}

asciistream
asciistream::createFromFile(stringref fileName)
{
    FastOS_File file(vespalib::string(fileName).c_str());
    asciistream is;
    if (file.OpenReadOnly()) {
        ssize_t sz = file.getSize();
        if (sz < 0) {
            throw IoException("Failed getting size of  file " + fileName + " : Error=" + file.getLastErrorString(), IoException::UNSPECIFIED, VESPA_STRLOC);
        }
        if (sz == 0) {
            return is;
        }
        alloc::Alloc buf = alloc::Alloc::alloc(sz);
        ssize_t actual = file.Read(buf.get(), sz);
        if (actual != sz) {
            asciistream e;
            e << "Failed reading " << sz << " bytes from file " << fileName;
            throw IoException(e.str() + " : Error=" + file.getLastErrorString(), IoException::UNSPECIFIED, VESPA_STRLOC);
        }
        is << stringref(static_cast<const char *>(buf.get()), sz);
    }
    return is;
}

asciistream
asciistream::createFromDevice(stringref fileName)
{
    FastOS_File file(vespalib::string(fileName).c_str());
    asciistream is;
    if (file.OpenReadOnly()) {
        constexpr size_t SZ = 64_Ki;
        auto buf = std::make_unique<char []>(SZ);
        for (ssize_t actual = file.Read(buf.get(), SZ); actual > 0; actual = file.Read(buf.get(), SZ)) {
            is << stringref(buf.get(), actual);
        }
    }
    return is;
}

ssize_t
getline(asciistream & is, string & line, char delim)
{
    line = is.getline(delim);
    return line.size();
}


}