Merge to XFA: Add ostream helpers for FX String classes.
authorTom Sepez <tsepez@chromium.org>
Wed, 7 Jan 2015 20:28:56 +0000 (12:28 -0800)
committerTom Sepez <tsepez@chromium.org>
Wed, 7 Jan 2015 20:28:56 +0000 (12:28 -0800)
Original CL at https://codereview.chromium.org/837843002

TBR=brucedawson@chromium.org

Review URL: https://codereview.chromium.org/809313008

core/include/fxcrt/fx_string.h
core/src/fxcrt/fx_basic_bstring_unittest.cpp
pdfium.gyp
testing/fx_string_testhelpers.cpp [new file with mode: 0644]
testing/fx_string_testhelpers.h [new file with mode: 0644]

index 91032f9..524d7ef 100644 (file)
@@ -18,6 +18,7 @@ class CFX_WideStringL;
 class CFX_ByteStringC : public CFX_Object
 {
 public:
+    typedef FX_CHAR value_type;
 
     CFX_ByteStringC()
     {
@@ -160,6 +161,7 @@ struct CFX_StringData {
 class CFX_ByteString : public CFX_Object
 {
 public:
+    typedef FX_CHAR value_type;
 
     CFX_ByteString()
     {
@@ -473,6 +475,7 @@ typedef CFX_StringBufTemplate<256> CFX_StringBuf256;
 class CFX_WideStringC : public CFX_Object
 {
 public:
+    typedef FX_WCHAR value_type;
 
     CFX_WideStringC()
     {
@@ -618,6 +621,7 @@ struct CFX_StringDataW {
 class CFX_WideString : public CFX_Object
 {
 public:
+    typedef FX_WCHAR value_type;
 
     CFX_WideString()
     {
index cf5e05d..138f1bf 100644 (file)
@@ -3,7 +3,7 @@
 // found in the LICENSE file.
 
 #include "testing/gtest/include/gtest/gtest.h"
-
+#include "testing/fx_string_testhelpers.h"
 #include "../../include/fxcrt/fx_basic.h"
 
 TEST(fxcrt, ByteStringCNull) {
index 1356291..1f852d6 100644 (file)
         '<(DEPTH)'
       ],
       'sources': [
+        'testing/fx_string_testhelpers.h',
+        'testing/fx_string_testhelpers.cpp',
         'core/src/fxcrt/fx_basic_bstring_unittest.cpp',
       ],
     },
diff --git a/testing/fx_string_testhelpers.cpp b/testing/fx_string_testhelpers.cpp
new file mode 100644 (file)
index 0000000..1ee705f
--- /dev/null
@@ -0,0 +1,47 @@
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "fx_string_testhelpers.h"
+
+#include <ios>
+#include <iomanip>
+
+namespace {
+
+template <typename T>
+std::ostream& output_string(std::ostream& out, const T& str) {
+  out << std::hex << std::setfill('0') << '"';
+  for (size_t i = 0; i < str.GetLength(); ++i) {
+    unsigned int c = str.GetAt(i);
+    if (c >= 0x20 && c < 0x7F) {
+      out << static_cast<char>(c);
+    } else if (sizeof(typename T::value_type) == 1) {
+      out << "\\x" << std::setw(2) << c << std::setw(0);
+    } else if (c < 0x10000) {
+      out << "\\u" << std::setw(4) << c << std::setw(0);
+    } else {
+      out << "<invalid>";
+    }
+  }
+  out << '"' << std::dec << std::setfill(' ');
+  return out;
+}
+
+}  // namespace
+
+std::ostream& operator<<(std::ostream& out, const CFX_ByteStringC& str) {
+  return output_string(out, str);
+}
+
+std::ostream& operator<<(std::ostream& out, const CFX_ByteString& str) {
+  return output_string(out, str);
+}
+
+std::ostream& operator<<(std::ostream& out, const CFX_WideStringC& str) {
+  return output_string(out, str);
+}
+
+std::ostream& operator<<(std::ostream& out, const CFX_WideString& str) {
+  return output_string(out, str);
+}
diff --git a/testing/fx_string_testhelpers.h b/testing/fx_string_testhelpers.h
new file mode 100644 (file)
index 0000000..7d06688
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2014 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef TESTING_FX_STRING_TESTHELPERS_H_
+#define TESTING_FX_STRING_TESTHELPERS_H_
+
+#include <ostream>
+
+#include "../core/include/fxcrt/fx_basic.h"
+
+// Output stream operator so GTEST macros work with FX strings.
+std::ostream& operator<<(std::ostream& out, const CFX_ByteStringC& str);
+std::ostream& operator<<(std::ostream& out, const CFX_ByteString& str);
+std::ostream& operator<<(std::ostream& out, const CFX_WideStringC& str);
+std::ostream& operator<<(std::ostream& out, const CFX_WideString& str);
+
+#endif  // TESTING_FX_STR_TESTHELPERS_H_
+