summaryrefslogtreecommitdiffstats
path: root/include/raul/Symbol.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'include/raul/Symbol.hpp')
-rw-r--r--include/raul/Symbol.hpp186
1 files changed, 97 insertions, 89 deletions
diff --git a/include/raul/Symbol.hpp b/include/raul/Symbol.hpp
index ff593ff..30917b5 100644
--- a/include/raul/Symbol.hpp
+++ b/include/raul/Symbol.hpp
@@ -34,96 +34,104 @@ namespace Raul {
*
* @ingroup raul
*/
-class Symbol : public std::basic_string<char> {
+class Symbol : public std::basic_string<char>
+{
public:
- /** Attempt to construct an invalid Symbol. */
- class BadSymbol : public Exception {
- public:
- explicit BadSymbol(const std::string& symbol) : Exception(symbol) {}
- };
-
- /** Construct a Symbol from a C++ string.
- *
- * This will throw an exception if `symbol` is invalid. To avoid this,
- * use is_valid() first to check.
- */
- explicit Symbol(const std::basic_string<char>& symbol)
- : std::basic_string<char>(symbol)
- {
- if (!is_valid(symbol)) {
- throw BadSymbol(symbol);
- }
- }
-
- /** Construct a Symbol from a C string.
- *
- * This will throw an exception if `symbol` is invalid. To avoid this,
- * use is_valid() first to check.
- */
- explicit Symbol(const char* symbol)
- : std::basic_string<char>(symbol)
- {
- if (!is_valid(symbol)) {
- throw BadSymbol(symbol);
- }
- }
-
- Symbol(const Symbol& symbol) = default;
- Symbol& operator=(const Symbol& symbol) = default;
-
- Symbol(Symbol&& symbol) = default;
- Symbol& operator=(Symbol&& symbol) = default;
-
- ~Symbol() = default;
-
- /** Return true iff `c` is a valid Symbol start character. */
- static inline bool is_valid_start_char(char c) {
- return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
- }
-
- /** Return true iff `c` is a valid Symbol character. */
- static inline bool is_valid_char(char c) {
- return is_valid_start_char(c) || (c >= '0' && c <= '9');
- }
-
- /** Return true iff `str` is a valid Symbol. */
- static inline bool is_valid(const std::basic_string<char>& str) {
- if (str.empty() || (str[0] >= '0' && str[0] <= '9')) {
- return false; // Must start with a letter or underscore
- }
-
- for (auto c : str) {
- if (!is_valid_char(c)) {
- return false; // All characters must be _, a-z, A-Z, 0-9
- }
- }
-
- return true;
- }
-
- /** Convert a string to a valid symbol.
- *
- * This will make a best effort at turning `str` into a complete, valid
- * Symbol, and will always return one.
- */
- static inline Symbol symbolify(const std::basic_string<char>& in) {
- if (in.empty()) {
- return Symbol("_");
- }
-
- std::basic_string<char> out(in);
- for (size_t i = 0; i < in.length(); ++i) {
- if (!is_valid_char(out[i])) {
- out[i] = '_';
- }
- }
-
- if (is_valid_start_char(out[0])) {
- return Symbol(out);
- }
-
- return Symbol(std::string("_") + out);
- }
+ /** Attempt to construct an invalid Symbol. */
+ class BadSymbol : public Exception
+ {
+ public:
+ explicit BadSymbol(const std::string& symbol)
+ : Exception(symbol)
+ {}
+ };
+
+ /** Construct a Symbol from a C++ string.
+ *
+ * This will throw an exception if `symbol` is invalid. To avoid this,
+ * use is_valid() first to check.
+ */
+ explicit Symbol(const std::basic_string<char>& symbol)
+ : std::basic_string<char>(symbol)
+ {
+ if (!is_valid(symbol)) {
+ throw BadSymbol(symbol);
+ }
+ }
+
+ /** Construct a Symbol from a C string.
+ *
+ * This will throw an exception if `symbol` is invalid. To avoid this,
+ * use is_valid() first to check.
+ */
+ explicit Symbol(const char* symbol)
+ : std::basic_string<char>(symbol)
+ {
+ if (!is_valid(symbol)) {
+ throw BadSymbol(symbol);
+ }
+ }
+
+ Symbol(const Symbol& symbol) = default;
+ Symbol& operator=(const Symbol& symbol) = default;
+
+ Symbol(Symbol&& symbol) = default;
+ Symbol& operator=(Symbol&& symbol) = default;
+
+ ~Symbol() = default;
+
+ /** Return true iff `c` is a valid Symbol start character. */
+ static inline bool is_valid_start_char(char c)
+ {
+ return (c == '_') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+ }
+
+ /** Return true iff `c` is a valid Symbol character. */
+ static inline bool is_valid_char(char c)
+ {
+ return is_valid_start_char(c) || (c >= '0' && c <= '9');
+ }
+
+ /** Return true iff `str` is a valid Symbol. */
+ static inline bool is_valid(const std::basic_string<char>& str)
+ {
+ if (str.empty() || (str[0] >= '0' && str[0] <= '9')) {
+ return false; // Must start with a letter or underscore
+ }
+
+ for (auto c : str) {
+ if (!is_valid_char(c)) {
+ return false; // All characters must be _, a-z, A-Z, 0-9
+ }
+ }
+
+ return true;
+ }
+
+ /** Convert a string to a valid symbol.
+ *
+ * This will make a best effort at turning `str` into a complete, valid
+ * Symbol, and will always return one.
+ */
+ static inline Symbol symbolify(const std::basic_string<char>& in)
+ {
+ if (in.empty()) {
+ return Symbol("_");
+ }
+
+ std::basic_string<char> out(in);
+ for (size_t i = 0; i < in.length(); ++i) {
+ if (!is_valid_char(out[i])) {
+ out[i] = '_';
+ }
+ }
+
+ if (is_valid_start_char(out[0])) {
+ return Symbol(out);
+ }
+
+ return Symbol(std::string("_") + out);
+ }
};
} // namespace Raul