1 | // Copyright 2009-2021 Intel Corporation |
2 | // SPDX-License-Identifier: Apache-2.0 |
3 | |
4 | #include "stringstream.h" |
5 | |
6 | namespace embree |
7 | { |
8 | static const std::string stringChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 _.,+-=:/*\\" ; |
9 | |
10 | /* creates map for fast categorization of characters */ |
11 | static void createCharMap(bool map[256], const std::string& chrs) { |
12 | for (size_t i=0; i<256; i++) map[i] = false; |
13 | for (size_t i=0; i<chrs.size(); i++) map[uint8_t(chrs[i])] = true; |
14 | } |
15 | |
16 | /* simple tokenizer */ |
17 | StringStream::StringStream(const Ref<Stream<int> >& cin, const std::string& seps, const std::string& endl, bool multiLine) |
18 | : cin(cin), endl(endl), multiLine(multiLine) |
19 | { |
20 | createCharMap(map: isSepMap,chrs: seps); |
21 | createCharMap(map: isValidCharMap,chrs: stringChars); |
22 | } |
23 | |
24 | std::string StringStream::next() |
25 | { |
26 | /* skip separators */ |
27 | while (cin->peek() != EOF) { |
28 | if (endl != "" && cin->peek() == '\n') { cin->drop(); return endl; } |
29 | if (multiLine && cin->peek() == '\\') { |
30 | cin->drop(); |
31 | if (cin->peek() == '\n') { cin->drop(); continue; } |
32 | cin->unget(); |
33 | } |
34 | if (!isSeparator(c: cin->peek())) break; |
35 | cin->drop(); |
36 | } |
37 | |
38 | /* parse everything until the next separator */ |
39 | std::vector<char> str; str.reserve(n: 64); |
40 | while (cin->peek() != EOF && !isSeparator(c: cin->peek())) { |
41 | int c = cin->get(); |
42 | if (!isValidChar(c)) throw std::runtime_error("invalid character " +std::string(1,c)+" in input" ); |
43 | str.push_back(x: (char)c); |
44 | } |
45 | str.push_back(x: 0); |
46 | return std::string(str.data()); |
47 | } |
48 | } |
49 | |