Skip to content

Commit f851118

Browse files
committed
Move escape_non_alnum() to util/string_utils.{h,cpp}
The function is generally useful for obtaining strings that contain only alphanumeric characters.
1 parent 7662ed0 commit f851118

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

jbmc/src/java_bytecode/java_string_literals.cpp

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,12 @@ Author: Chris Smowton, [email protected]
1515
#include <util/arith_tools.h>
1616
#include <util/expr_initializer.h>
1717
#include <util/namespace.h>
18+
#include <util/string_utils.h>
1819
#include <util/unicode.h>
1920

2021
#include <iomanip>
2122
#include <sstream>
2223

23-
/// Replace non-alphanumeric characters with `_xx` escapes, where xx are hex
24-
/// digits. Underscores are replaced by `__`.
25-
/// \param to_escape: string to escape
26-
/// \return string with non-alphanumeric characters escaped
27-
static std::string escape_non_alnum(const std::string &to_escape)
28-
{
29-
std::ostringstream escaped;
30-
for(auto &ch : to_escape)
31-
{
32-
if(ch=='_')
33-
escaped << "__";
34-
else if(isalnum(ch))
35-
escaped << ch;
36-
else
37-
escaped << '_'
38-
<< std::hex
39-
<< std::setfill('0')
40-
<< std::setw(2)
41-
<< (unsigned int)ch;
42-
}
43-
return escaped.str();
44-
}
45-
4624
/// Convert UCS-2 or UTF-16 to an array expression.
4725
/// \par parameters: `in`: wide string to convert
4826
/// \return Returns a Java char array containing the same wchars.

src/util/string_utils.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Author: Daniel Poetzl
1010
#include "exception_utils.h"
1111
#include "invariant.h"
1212

13+
#include <algorithm>
1314
#include <cassert>
1415
#include <cctype>
15-
#include <algorithm>
16+
#include <iomanip>
1617

1718
/// Remove all whitespace characters from either end of a string. Whitespace
1819
/// in the middle of the string is left unchanged
@@ -159,3 +160,19 @@ std::string escape(const std::string &s)
159160

160161
return result;
161162
}
163+
164+
std::string escape_non_alnum(const std::string &to_escape)
165+
{
166+
std::ostringstream escaped;
167+
for(auto &ch : to_escape)
168+
{
169+
if(ch == '_')
170+
escaped << "__";
171+
else if(isalnum(ch))
172+
escaped << ch;
173+
else
174+
escaped << '_' << std::hex << std::setfill('0') << std::setw(2)
175+
<< (unsigned int)ch;
176+
}
177+
return escaped.str();
178+
}

src/util/string_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,10 @@ join_strings(Stream &&os, const It b, const It e, const Delimiter &delimiter)
9696
/// programming language.
9797
std::string escape(const std::string &);
9898

99+
/// Replace non-alphanumeric characters with `_xx` escapes, where xx are hex
100+
/// digits. Underscores are replaced by `__`.
101+
/// \param to_escape: string to escape
102+
/// \return string with non-alphanumeric characters escaped
103+
std::string escape_non_alnum(const std::string &to_escape);
104+
99105
#endif

0 commit comments

Comments
 (0)