stringstream.cpp 1.6 KB

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