diff options
| author | Chris Lattner <sabre@nondot.org> | 2001-10-29 13:28:00 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2001-10-29 13:28:00 +0000 |
| commit | 8c857b86fdc70a7f9cd24f7444ec3679972c0982 (patch) | |
| tree | eb270f6dfc965755203293cff2c859bdd076271f /llvm | |
| parent | 239a1bdfe19a378ca92df1f8b715587954860796 (diff) | |
| download | bcm5719-llvm-8c857b86fdc70a7f9cd24f7444ec3679972c0982.tar.gz bcm5719-llvm-8c857b86fdc70a7f9cd24f7444ec3679972c0982.zip | |
Checkin of C string stuff. Fix several bugs, including most escape codes being
printed as '\\n' instead of their correct code. Also print things in octal instead of hex.
llvm-svn: 1022
Diffstat (limited to 'llvm')
| -rw-r--r-- | llvm/support/lib/Support/StringExtras.cpp | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/llvm/support/lib/Support/StringExtras.cpp b/llvm/support/lib/Support/StringExtras.cpp new file mode 100644 index 00000000000..2229df049ac --- /dev/null +++ b/llvm/support/lib/Support/StringExtras.cpp @@ -0,0 +1,66 @@ + + +#include "llvm/Support/StringExtras.h" +#include "llvm/ConstPoolVals.h" +#include "llvm/DerivedTypes.h" + +// Can we treat the specified array as a string? Only if it is an array of +// ubytes or non-negative sbytes. +// +bool isStringCompatible(ConstPoolArray *CPA) { + const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType(); + if (ETy == Type::UByteTy) return true; + if (ETy != Type::SByteTy) return false; + + for (unsigned i = 0; i < CPA->getNumOperands(); ++i) + if (cast<ConstPoolSInt>(CPA->getOperand(i))->getValue() < 0) + return false; + + return true; +} + +// toOctal - Convert the low order bits of X into an octal letter +static inline char toOctal(int X) { + return (X&7)+'0'; +} + +// getAsCString - Return the specified array as a C compatible string, only if +// the predicate isStringCompatible is true. +// +string getAsCString(ConstPoolArray *CPA) { + if (isStringCompatible(CPA)) { + string Result; + const Type *ETy = cast<ArrayType>(CPA->getType())->getElementType(); + Result = "\""; + for (unsigned i = 0; i < CPA->getNumOperands(); ++i) { + unsigned char C = (ETy == Type::SByteTy) ? + (unsigned char)cast<ConstPoolSInt>(CPA->getOperand(i))->getValue() : + (unsigned char)cast<ConstPoolUInt>(CPA->getOperand(i))->getValue(); + + if (isprint(C)) { + Result += C; + } else { + switch(C) { + case '\a': Result += "\\a"; break; + case '\b': Result += "\\b"; break; + case '\f': Result += "\\f"; break; + case '\n': Result += "\\n"; break; + case '\r': Result += "\\r"; break; + case '\t': Result += "\\t"; break; + case '\v': Result += "\\v"; break; + default: + Result += '\\'; + Result += toOctal(C >> 6); + Result += toOctal(C >> 3); + Result += toOctal(C >> 0); + break; + } + } + } + Result += "\""; + + return Result; + } else { + return CPA->getStrValue(); + } +} |

