From bd0214b1da66225f410641692e89e492f668472a Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 2 Jan 2021 14:46:29 +0100 Subject: Format all code with clang-format --- include/raul/Symbol.hpp | 186 +++++++++++++++++++++++++----------------------- 1 file changed, 97 insertions(+), 89 deletions(-) (limited to 'include/raul/Symbol.hpp') 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 { +class Symbol : public std::basic_string +{ 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& symbol) - : std::basic_string(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(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& 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& in) { - if (in.empty()) { - return Symbol("_"); - } - - std::basic_string 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& symbol) + : std::basic_string(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(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& 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& in) + { + if (in.empty()) { + return Symbol("_"); + } + + std::basic_string 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 -- cgit v1.2.1