Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
Loading...
Searching...
No Matches
naming.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/naming.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/naming.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/naming.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22
49
50#pragma once
51
52#include <string>
53#include <stdexcept>
54
55#include <boost/regex/pending/unicode_iterator.hpp>
56#include <boost/spirit/home/support/char_encoding/unicode.hpp>
57
58namespace bertini {
59
60namespace detail {
61
64inline bool IsEmojiBase(char32_t cp)
65{
66 return (cp >= 0x1F000 && cp <= 0x1FAFF) // emoticons, pictographs, transport, supplemental & extended-A (incl. skin tones, regional indicators)
67 || (cp >= 0x2600 && cp <= 0x27BF) // miscellaneous symbols + dingbats (☀ ❤ ✨ ✅ ...)
68 || (cp >= 0x2B00 && cp <= 0x2BFF); // miscellaneous symbols and arrows (⭐ ⬅ ...)
69}
70
73inline bool IsEmojiGlue(char32_t cp)
74{
75 return cp == 0x200D // ZERO WIDTH JOINER (👩‍👩‍👧)
76 || cp == 0xFE0E || cp == 0xFE0F // variation selectors 15 / 16 (❤️)
77 || cp == 0x20E3; // combining enclosing keycap
78 // skin-tone modifiers (U+1F3FB..FF) and regional indicators (U+1F1E6..FF) are
79 // already covered by IsEmojiBase, so they continue a name via that predicate.
80}
81
82} // namespace detail
83
86inline bool IsIdentStart(char32_t cp)
87{
88 return boost::spirit::char_encoding::unicode::isalpha(cp)
90}
91
95inline bool IsIdentCont(char32_t cp)
96{
97 return boost::spirit::char_encoding::unicode::isalnum(cp)
98 || cp == U'[' || cp == U']' || cp == U'_'
100}
101
106inline bool IsValidVariableName(std::string const& s)
107{
108 if (s.empty())
109 return false;
110 try
111 {
112 boost::u8_to_u32_iterator<std::string::const_iterator> it(s.begin(), s.begin(), s.end());
113 boost::u8_to_u32_iterator<std::string::const_iterator> end(s.end(), s.begin(), s.end());
114 if (it == end || !IsIdentStart(*it))
115 return false;
116 for (++it; it != end; ++it)
117 if (!IsIdentCont(*it))
118 return false;
119 return true;
120 }
121 catch (...)
122 {
123 return false; // malformed UTF-8
124 }
125}
126
129inline void ThrowIfInvalidVariableName(std::string const& s)
130{
131 if (!IsValidVariableName(s))
132 throw std::runtime_error(
133 "invalid variable name \"" + s + "\": a name must be a nonempty identifier -- "
134 "it starts with a letter (ASCII or Unicode, e.g. x or \xCE\xA9) and continues with "
135 "letters, digits, or [ ] _ . Expressions, operators, whitespace, and leading "
136 "digits are not allowed.");
137}
138
139} // namespace bertini
Definition configured.hpp:34
bool IsEmojiGlue(char32_t cp)
True if cp only extends an emoji cluster – valid mid-name to glue a multi-code-point emoji,...
Definition naming.hpp:73
bool IsEmojiBase(char32_t cp)
True if cp is in a primary emoji / pictographic range, so a name may start with it (🎉,...
Definition naming.hpp:64
Definition algorithm_builder.hpp:36
bool IsIdentStart(char32_t cp)
True if code point cp may START an identifier: any Unicode letter (ASCII A-Z a-z, plus Ω,...
Definition naming.hpp:86
bool IsValidVariableName(std::string const &s)
True if s is a well-formed identifier: nonempty, a valid UTF-8 IsIdentStart code point followed by ze...
Definition naming.hpp:106
bool IsIdentCont(char32_t cp)
True if code point cp may CONTINUE an identifier: any Unicode alphanumeric, one of the legacy continu...
Definition naming.hpp:95
void ThrowIfInvalidVariableName(std::string const &s)
Throw std::runtime_error if s is not a valid variable name (.
Definition naming.hpp:129