[Scummvm-git-logs] scummvm master -> f6e33231bb0e0f9ebf40cdb2b096b1412f1c44c1
sev-
sev at scummvm.org
Tue Jun 29 20:15:45 UTC 2021
This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .
Summary:
f6e33231bb COMMON: Add an optional parameter to tag2str() for printing non-printables
Commit: f6e33231bb0e0f9ebf40cdb2b096b1412f1c44c1
https://github.com/scummvm/scummvm/commit/f6e33231bb0e0f9ebf40cdb2b096b1412f1c44c1
Author: Eugene Sandulenko (sev at scummvm.org)
Date: 2021-06-29T22:15:02+02:00
Commit Message:
COMMON: Add an optional parameter to tag2str() for printing non-printables
This time without variadic macros
Changed paths:
common/str.cpp
common/str.h
diff --git a/common/str.cpp b/common/str.cpp
index fbd9a03744..06876598de 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -689,19 +689,24 @@ void replace(Common::String &source, const Common::String &what, const Common::S
}
}
-String tag2string(uint32 tag) {
- char str[5];
- str[0] = (char)(tag >> 24);
- str[1] = (char)(tag >> 16);
- str[2] = (char)(tag >> 8);
- str[3] = (char)tag;
- str[4] = '\0';
- // Replace non-printable chars by dot
- for (int i = 0; i < 4; ++i) {
- if (!Common::isPrint(str[i]))
- str[i] = '.';
+String tag2string(uint32 tag, bool nonPrintable) {
+ Common::String res;
+
+ for (int i = 3; i >= 0; i--) {
+ byte b = (tag >> (8 * i)) & 0xff;
+
+ if (!Common::isPrint(b)) {
+ if (nonPrintable) {
+ res += Common::String::format("\\%03o", b);
+ } else {
+ res += '.';
+ }
+ } else {
+ res += b;
+ }
}
- return String(str);
+
+ return res;
}
#endif
diff --git a/common/str.h b/common/str.h
index 8ad41d92e1..1ceef848e9 100644
--- a/common/str.h
+++ b/common/str.h
@@ -349,8 +349,11 @@ void replace(Common::String &source, const Common::String &what, const Common::S
* Take a 32 bit value and turn it into a four character string, where each of
* the four bytes is turned into one character. Most significant byte is printed
* first.
+ *
+ * @param tag tag value to convert
+ * @param nonPrintable indicate if non-printable characters need to be printed as octals
*/
-String tag2string(uint32 tag);
+String tag2string(uint32 tag, bool nonPrintable = false);
/**
* Copy up to size - 1 characters from src to dst and also zero terminate the
@@ -407,6 +410,13 @@ size_t strnlen(const char *src, size_t maxSize);
*/
#define tag2str(x) Common::tag2string(x).c_str()
+/**
+ * Convenience wrapper for tag2string with non-printable characters which "returns" a C string.
+ * Note: It is *NOT* safe to do anything with the return value other than directly
+ * copying or printing it.
+ */
+#define tag2strP(x) Common::tag2string(x, true).c_str()
+
/**
* Converts string with all non-printable characters properly escaped
* with use of C++ escape sequences
More information about the Scummvm-git-logs
mailing list