string.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #pragma once
  2. #include <optional>
  3. #include <string>
  4. #include <vector>
  5. namespace jinja {
  6. // allow differentiate between user input strings and template strings
  7. // transformations should handle this information as follows:
  8. // - one-to-one (e.g., uppercase, lowercase): preserve is_input flag
  9. // - one-to-many (e.g., strip): if input string is marked as is_input, all resulting parts should be marked as is_input
  10. // - many-to-one (e.g., concat): if ALL input parts are marked as is_input, resulting part should be marked as is_input
  11. struct string_part {
  12. bool is_input = false; // may skip parsing special tokens if true
  13. std::string val;
  14. bool is_uppercase() const;
  15. bool is_lowercase() const;
  16. };
  17. struct string {
  18. std::vector<string_part> parts;
  19. string() = default;
  20. string(const std::string & v, bool user_input = false) {
  21. parts.push_back({user_input, v});
  22. }
  23. string(int v) {
  24. parts.push_back({false, std::to_string(v)});
  25. }
  26. string(double v) {
  27. parts.push_back({false, std::to_string(v)});
  28. }
  29. // mark all parts as user input
  30. void mark_input();
  31. std::string str() const;
  32. size_t length() const;
  33. bool all_parts_are_input() const;
  34. bool is_uppercase() const;
  35. bool is_lowercase() const;
  36. // mark this string as input if other has ALL parts as input
  37. void mark_input_based_on(const string & other);
  38. string append(const string & other);
  39. // in-place transformations
  40. string uppercase();
  41. string lowercase();
  42. string capitalize();
  43. string titlecase();
  44. string strip(bool left, bool right, std::optional<const std::string_view> chars = std::nullopt);
  45. };
  46. } // namespace jinja