1// Copyright 2009-2021 Intel Corporation
2// SPDX-License-Identifier: Apache-2.0
3
4#include "stringstream.h"
5
6namespace 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(isSepMap,seps);
21 createCharMap(isValidCharMap,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(cin->peek())) break;
35 cin->drop();
36 }
37
38 /* parse everything until the next separator */
39 std::vector<char> str; str.reserve(64);
40 while (cin->peek() != EOF && !isSeparator(cin->peek())) {
41 int c = cin->get();
42 // -- GODOT start --
43 // if (!isValidChar(c)) throw std::runtime_error("invalid character "+std::string(1,c)+" in input");
44 if (!isValidChar(c)) abort();
45 // -- GODOT end --
46 str.push_back((char)c);
47 }
48 str.push_back(0);
49 return std::string(str.data());
50 }
51}
52