Skip to content

Commit

Permalink
api(strutil.h): Strutil::string_is_identifier() (#4333)
Browse files Browse the repository at this point in the history
Signed-off-by: Larry Gritz <[email protected]>
  • Loading branch information
lgritz committed Jul 16, 2024
1 parent 48f7efa commit 6a3503a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/include/OpenImageIO/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,14 @@ string_view OIIO_UTIL_API parse_line(string_view& str, bool eat = true) noexcept
/// match.
string_view OIIO_UTIL_API parse_nested (string_view &str, bool eat=true) noexcept;

/// Does the string follow the lexical rule of a C identifier?
inline bool
string_is_identifier(string_view str)
{
// If a leading identifier is the entirety of str, it's an ident.
string_view ident = parse_identifier(str);
return (!ident.empty() && str.empty());
}

/// Look within `str` for the pattern:
/// head nonwhitespace_chars whitespace
Expand Down
10 changes: 10 additions & 0 deletions src/libutil/strutil_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,16 @@ void test_parse ()
OIIO_CHECK_EQUAL (ss, ""); OIIO_CHECK_EQUAL (s, "");
s = "(blah"; ss = parse_nested (s);
OIIO_CHECK_EQUAL (ss, ""); OIIO_CHECK_EQUAL (s, "(blah");

OIIO_CHECK_EQUAL(string_is_identifier("valid"), true);
OIIO_CHECK_EQUAL(string_is_identifier("_underscore"), true);
OIIO_CHECK_EQUAL(string_is_identifier("with123numbers"), true);
OIIO_CHECK_EQUAL(string_is_identifier("123invalidStart"), false);
OIIO_CHECK_EQUAL(string_is_identifier("invalid-char"), false);
OIIO_CHECK_EQUAL(string_is_identifier(""), false);
OIIO_CHECK_EQUAL(string_is_identifier("a"), true);
OIIO_CHECK_EQUAL(string_is_identifier("_"), true);
OIIO_CHECK_EQUAL(string_is_identifier("1"), false);
}


Expand Down

0 comments on commit 6a3503a

Please sign in to comment.