summaryrefslogtreecommitdiffstats
path: root/chrome/common/mac
diff options
context:
space:
mode:
authornoyau@chromium.org <noyau@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-22 16:41:19 +0000
committernoyau@chromium.org <noyau@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-10-22 16:41:19 +0000
commitd9b6d57c79b698a2a3b00b0842b77db53f1c961e (patch)
treec29d404ec809b28a14f8450c278519e6fbc1f68c /chrome/common/mac
parentbf9612fa2fcf1a06c8d91e2870e8eca5635fae0a (diff)
downloadchromium_src-d9b6d57c79b698a2a3b00b0842b77db53f1c961e.zip
chromium_src-d9b6d57c79b698a2a3b00b0842b77db53f1c961e.tar.gz
chromium_src-d9b6d57c79b698a2a3b00b0842b77db53f1c961e.tar.bz2
NSCoder support for std::string archiving.
A small NSCoder extension to save a std::string in an archive. BUG=None Review URL: https://chromiumcodereview.appspot.com/11231005 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@163306 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/mac')
-rw-r--r--chrome/common/mac/nscoder_util.h22
-rw-r--r--chrome/common/mac/nscoder_util.mm24
-rw-r--r--chrome/common/mac/nscoder_util_unittest.mm57
3 files changed, 103 insertions, 0 deletions
diff --git a/chrome/common/mac/nscoder_util.h b/chrome/common/mac/nscoder_util.h
new file mode 100644
index 0000000..7e73f24
--- /dev/null
+++ b/chrome/common/mac/nscoder_util.h
@@ -0,0 +1,22 @@
+// Copyright (c) 2012 The Chromium 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 CHROME_COMMON_MAC_NSCODER_UTIL_H_
+#define CHROME_COMMON_MAC_NSCODER_UTIL_H_
+
+#import <Foundation/Foundation.h>
+
+#include <string>
+
+namespace nscoder_util {
+
+// Archives a std::string in an Objective-C key archiver.
+void EncodeString(NSCoder* coder, NSString* key, const std::string& string);
+
+// Decode a std::string from an Objective-C key unarchiver.
+std::string DecodeString(NSCoder* decoder, NSString* key);
+
+} // namespace nscoder_util
+
+#endif // CHROME_COMMON_MAC_NSCODER_UTIL_H_
diff --git a/chrome/common/mac/nscoder_util.mm b/chrome/common/mac/nscoder_util.mm
new file mode 100644
index 0000000..6fb1b1c
--- /dev/null
+++ b/chrome/common/mac/nscoder_util.mm
@@ -0,0 +1,24 @@
+// Copyright (c) 2012 The Chromium 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 <string>
+
+#include "chrome/common/mac/nscoder_util.h"
+
+namespace nscoder_util {
+
+void EncodeString(NSCoder* coder, NSString* key, const std::string& string) {
+ [coder encodeBytes:reinterpret_cast<const uint8_t*>(string.data())
+ length:string.size()
+ forKey:key];
+}
+
+std::string DecodeString(NSCoder* decoder, NSString* key) {
+ NSUInteger length;
+ const uint8_t* bytes = [decoder decodeBytesForKey:key
+ returnedLength:&length];
+ return std::string(reinterpret_cast<const char*>(bytes), length);
+}
+
+} // namespace nscoder_util
diff --git a/chrome/common/mac/nscoder_util_unittest.mm b/chrome/common/mac/nscoder_util_unittest.mm
new file mode 100644
index 0000000..204ebd7
--- /dev/null
+++ b/chrome/common/mac/nscoder_util_unittest.mm
@@ -0,0 +1,57 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#import <Foundation/Foundation.h>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_nsobject.h"
+#include "chrome/common/mac/nscoder_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/platform_test.h"
+
+namespace {
+
+typedef PlatformTest NSCoderStdStringTest;
+
+const char* testStrings[] = {
+ "Arf",
+ "",
+ "This is working™",
+ "古池や蛙飛込む水の音\nふるいけやかわずとびこむみずのおと",
+ "ἀγεωμέτρητος μηδεὶς εἰσίτω",
+ "Bang!\t\n"
+};
+
+TEST_F(NSCoderStdStringTest, encodeDecode) {
+ for (size_t i = 0; i < arraysize(testStrings); ++i) {
+ NSMutableData *data = [NSMutableData data];
+
+ scoped_nsobject<NSKeyedArchiver> archiver(
+ [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]);
+ nscoder_util::EncodeString(archiver, @"test", testStrings[i]);
+ [archiver finishEncoding];
+
+ scoped_nsobject<NSKeyedUnarchiver> unarchiver(
+ [[NSKeyedUnarchiver alloc] initForReadingWithData:data]);
+ const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test");
+
+ EXPECT_EQ(decoded, testStrings[i]);
+ }
+}
+
+TEST_F(NSCoderStdStringTest, decodeEmpty) {
+ NSMutableData *data = [NSMutableData data];
+
+ scoped_nsobject<NSKeyedArchiver> archiver(
+ [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]);
+ [archiver finishEncoding];
+
+ scoped_nsobject<NSKeyedUnarchiver> unarchiver(
+ [[NSKeyedUnarchiver alloc] initForReadingWithData:data]);
+ const std::string decoded = nscoder_util::DecodeString(unarchiver, @"test");
+
+ EXPECT_EQ(decoded, "");
+}
+
+} // namespace