Debian Codegraph @

src/kj/parse/char.c++

raw symbols SCIP
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 #include "char.h"
23 #include "../debug.h"
24 #include <stdlib.h>
25
26 namespace kj {
27 namespace parse {
28
29 namespace _ { // private
30
31 double ParseFloat::operator()(const Array<char>& digits,
32 const Maybe<Array<char>>& fraction,
33 const Maybe<Tuple<Maybe<char>, Array<char>>>& exponent) const {
34 size_t bufSize = digits.size();
35 KJ_IF_MAYBE(f, fraction) {
36 bufSize += 1 + f->size();
37 }
38 KJ_IF_MAYBE(e, exponent) {
39 bufSize += 1 + (get<0>(*e) != nullptr) + get<1>(*e).size();
40 }
41
42 KJ_STACK_ARRAY(char, buf, bufSize + 1, 128, 128);
43
44 char* pos = buf.begin();
45 memcpy(pos, digits.begin(), digits.size());
46 pos += digits.size();
47 KJ_IF_MAYBE(f, fraction) {
48 *pos++ = '.';
49 memcpy(pos, f->begin(), f->size());
50 pos += f->size();
51 }
52 KJ_IF_MAYBE(e, exponent) {
53 *pos++ = 'e';
54 KJ_IF_MAYBE(sign, get<0>(*e)) {
55 *pos++ = *sign;
56 }
57 memcpy(pos, get<1>(*e).begin(), get<1>(*e).size());
58 pos += get<1>(*e).size();
59 }
60
61 *pos++ = '\0';
62 KJ_DASSERT(pos == buf.end());
63
64 // The above construction should always produce a valid double, so this should never throw...
65 return StringPtr(buf.begin(), bufSize).parseAs<double>();
66 }
67
68 } // namespace _ (private)
69
70 } // namespace parse
71 } // namespace kj