summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/llvm/ADT/StringExtras.h9
-rw-r--r--include/llvm/Support/raw_ostream.h2
-rw-r--r--lib/MC/MCAsmStreamer.cpp2
-rw-r--r--lib/ProfileData/InstrProfReader.cpp2
-rw-r--r--lib/Support/StringExtras.cpp2
-rw-r--r--lib/Support/raw_ostream.cpp4
-rw-r--r--lib/Support/regengine.inc2
-rw-r--r--tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp4
-rw-r--r--tools/llvm-objdump/llvm-objdump.cpp4
-rw-r--r--tools/llvm-readobj/ObjDumper.cpp4
-rw-r--r--unittests/ADT/StringExtrasTest.cpp18
11 files changed, 40 insertions, 13 deletions
diff --git a/include/llvm/ADT/StringExtras.h b/include/llvm/ADT/StringExtras.h
index b1596c54da5..71b0e7527cb 100644
--- a/include/llvm/ADT/StringExtras.h
+++ b/include/llvm/ADT/StringExtras.h
@@ -99,6 +99,15 @@ inline bool isASCII(llvm::StringRef S) {
return true;
}
+/// Checks whether character \p C is printable.
+///
+/// Locale-independent version of the C standard library isprint whose results
+/// may differ on different platforms.
+inline bool isPrint(char C) {
+ unsigned char UC = static_cast<unsigned char>(C);
+ return (0x20 <= UC) && (UC <= 0x7E);
+}
+
/// Returns the corresponding lowercase character if \p x is uppercase.
inline char toLower(char x) {
if (x >= 'A' && x <= 'Z')
diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h
index e54c8cc5d3e..b9ea9b5817f 100644
--- a/include/llvm/Support/raw_ostream.h
+++ b/include/llvm/Support/raw_ostream.h
@@ -220,7 +220,7 @@ public:
raw_ostream &write_uuid(const uuid_t UUID);
/// Output \p Str, turning '\\', '\t', '\n', '"', and anything that doesn't
- /// satisfy std::isprint into an escape sequence.
+ /// satisfy llvm::isPrint into an escape sequence.
raw_ostream &write_escaped(StringRef Str, bool UseHexEscapes = false);
raw_ostream &write(unsigned char C);
diff --git a/lib/MC/MCAsmStreamer.cpp b/lib/MC/MCAsmStreamer.cpp
index 1429f647b61..88154bd7b25 100644
--- a/lib/MC/MCAsmStreamer.cpp
+++ b/lib/MC/MCAsmStreamer.cpp
@@ -815,7 +815,7 @@ static void PrintQuotedString(StringRef Data, raw_ostream &OS) {
continue;
}
- if (isprint((unsigned char)C)) {
+ if (isPrint((unsigned char)C)) {
OS << (char)C;
continue;
}
diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp
index 64f98ad0051..3b704158a5c 100644
--- a/lib/ProfileData/InstrProfReader.cpp
+++ b/lib/ProfileData/InstrProfReader.cpp
@@ -129,7 +129,7 @@ bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
StringRef buffer = Buffer.getBufferStart();
return count == 0 ||
std::all_of(buffer.begin(), buffer.begin() + count,
- [](char c) { return ::isprint(c) || ::isspace(c); });
+ [](char c) { return isPrint(c) || ::isspace(c); });
}
// Read the profile variant flag from the header: ":FE" means this is a FE
diff --git a/lib/Support/StringExtras.cpp b/lib/Support/StringExtras.cpp
index d566b72a8d0..386d74a4798 100644
--- a/lib/Support/StringExtras.cpp
+++ b/lib/Support/StringExtras.cpp
@@ -61,7 +61,7 @@ void llvm::SplitString(StringRef Source,
void llvm::printEscapedString(StringRef Name, raw_ostream &Out) {
for (unsigned i = 0, e = Name.size(); i != e; ++i) {
unsigned char C = Name[i];
- if (isprint(C) && C != '\\' && C != '"')
+ if (isPrint(C) && C != '\\' && C != '"')
Out << C;
else
Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp
index b2260c1865e..1dae469958f 100644
--- a/lib/Support/raw_ostream.cpp
+++ b/lib/Support/raw_ostream.cpp
@@ -160,7 +160,7 @@ raw_ostream &raw_ostream::write_escaped(StringRef Str,
*this << '\\' << '"';
break;
default:
- if (std::isprint(c)) {
+ if (isPrint(c)) {
*this << c;
break;
}
@@ -436,7 +436,7 @@ raw_ostream &raw_ostream::operator<<(const FormattedBytes &FB) {
// Print the ASCII char values for each byte on this line
for (uint8_t Byte : Line) {
- if (isprint(Byte))
+ if (isPrint(Byte))
*this << static_cast<char>(Byte);
else
*this << '.';
diff --git a/lib/Support/regengine.inc b/lib/Support/regengine.inc
index 62d8c267f22..41787aff124 100644
--- a/lib/Support/regengine.inc
+++ b/lib/Support/regengine.inc
@@ -1013,7 +1013,7 @@ pchar(int ch)
{
static char pbuf[10];
- if (isprint(ch) || ch == ' ')
+ if (isPrint(ch) || ch == ' ')
(void)snprintf(pbuf, sizeof pbuf, "%c", ch);
else
(void)snprintf(pbuf, sizeof pbuf, "\\%o", ch);
diff --git a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
index 4551718344c..1939dc6440f 100644
--- a/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
+++ b/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
@@ -699,7 +699,7 @@ static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
std::string Str;
bool ArrayIsPrintable = true;
for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
- if (!isprint(static_cast<unsigned char>(Record[j]))) {
+ if (!isPrint(static_cast<unsigned char>(Record[j]))) {
ArrayIsPrintable = false;
break;
}
@@ -719,7 +719,7 @@ static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
} else {
bool BlobIsPrintable = true;
for (unsigned i = 0, e = Blob.size(); i != e; ++i)
- if (!isprint(static_cast<unsigned char>(Blob[i]))) {
+ if (!isPrint(static_cast<unsigned char>(Blob[i]))) {
BlobIsPrintable = false;
break;
}
diff --git a/tools/llvm-objdump/llvm-objdump.cpp b/tools/llvm-objdump/llvm-objdump.cpp
index 610cd11365c..8041e6f5994 100644
--- a/tools/llvm-objdump/llvm-objdump.cpp
+++ b/tools/llvm-objdump/llvm-objdump.cpp
@@ -1651,7 +1651,7 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
}
Byte = Bytes.slice(Index)[0];
outs() << format(" %02x", Byte);
- AsciiData[NumBytes] = isprint(Byte) ? Byte : '.';
+ AsciiData[NumBytes] = isPrint(Byte) ? Byte : '.';
uint8_t IndentOffset = 0;
NumBytes++;
@@ -1899,7 +1899,7 @@ void llvm::PrintSectionContents(const ObjectFile *Obj) {
// Print ascii.
outs() << " ";
for (std::size_t i = 0; i < 16 && addr + i < end; ++i) {
- if (std::isprint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
+ if (isPrint(static_cast<unsigned char>(Contents[addr + i]) & 0xFF))
outs() << Contents[addr + i];
else
outs() << ".";
diff --git a/tools/llvm-readobj/ObjDumper.cpp b/tools/llvm-readobj/ObjDumper.cpp
index 17675fd8f27..a725140c9d3 100644
--- a/tools/llvm-readobj/ObjDumper.cpp
+++ b/tools/llvm-readobj/ObjDumper.cpp
@@ -29,7 +29,7 @@ ObjDumper::~ObjDumper() {
static void printAsPrintable(raw_ostream &W, const uint8_t *Start, size_t Len) {
for (size_t i = 0; i < Len; i++)
- W << (isprint(Start[i]) ? static_cast<char>(Start[i]) : '.');
+ W << (isPrint(Start[i]) ? static_cast<char>(Start[i]) : '.');
}
static Expected<object::SectionRef>
@@ -138,7 +138,7 @@ void ObjDumper::printSectionAsHex(const object::ObjectFile *Obj,
TmpSecPtr = SecPtr;
for (i = 0; TmpSecPtr + i < SecEnd && i < 16; ++i)
- W.startLine() << (isprint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
+ W.startLine() << (isPrint(TmpSecPtr[i]) ? static_cast<char>(TmpSecPtr[i])
: '.');
W.startLine() << '\n';
diff --git a/unittests/ADT/StringExtrasTest.cpp b/unittests/ADT/StringExtrasTest.cpp
index f98f388f64f..1df200553a6 100644
--- a/unittests/ADT/StringExtrasTest.cpp
+++ b/unittests/ADT/StringExtrasTest.cpp
@@ -13,6 +13,17 @@
using namespace llvm;
+TEST(StringExtrasTest, isPrint) {
+ EXPECT_FALSE(isPrint('\0'));
+ EXPECT_FALSE(isPrint('\t'));
+ EXPECT_TRUE(isPrint('0'));
+ EXPECT_TRUE(isPrint('a'));
+ EXPECT_TRUE(isPrint('A'));
+ EXPECT_TRUE(isPrint(' '));
+ EXPECT_TRUE(isPrint('~'));
+ EXPECT_TRUE(isPrint('?'));
+}
+
TEST(StringExtrasTest, Join) {
std::vector<std::string> Items;
EXPECT_EQ("", join(Items.begin(), Items.end(), " <sep> "));
@@ -93,6 +104,13 @@ TEST(StringExtrasTest, printLowerCase) {
EXPECT_EQ("abcdefg01234.,&!~`'}\"", OS.str());
}
+TEST(StringExtrasTest, printEscapedString) {
+ std::string str;
+ raw_string_ostream OS(str);
+ printEscapedString("ABCdef123&<>\\\"'\t", OS);
+ EXPECT_EQ("ABCdef123&<>\\5C\\22'\\09", OS.str());
+}
+
TEST(StringExtrasTest, printHTMLEscaped) {
std::string str;
raw_string_ostream OS(str);