Permalink
Browse files

Implement StaticString::find() and StaticString::substr().

  • Loading branch information...
1 parent 5e7722b commit f79a9f7f3dbf04ec2a80391e5889f5d2d6850c0a @FooBarWidget FooBarWidget committed Nov 24, 2010
Showing with 97 additions and 0 deletions.
  1. +25 −0 ext/common/StaticString.h
  2. +72 −0 test/cxx/StaticStringTest.cpp
View
@@ -28,6 +28,7 @@
#include <string>
#include <cstring>
#include <ostream>
+#include <stdexcept>
namespace Passenger {
@@ -121,6 +122,30 @@ class StaticString {
return len == other.size() && memcmp(content, other.data(), len) == 0;
}
+ string::size_type find(char c, string::size_type pos = 0) const {
+ if (pos < len) {
+ const char *result = (const char *) memchr(content + pos, c, len - pos);
+ if (result == NULL) {
+ return string::npos;
+ } else {
+ return result - content;
+ }
+ } else {
+ return string::npos;
+ }
+ }
+
+ StaticString substr(string::size_type pos = 0, string::size_type n = string::npos) const {
+ if (pos > len) {
+ throw out_of_range("Argument 'pos' out of range");
+ } else {
+ if (n > len - pos) {
+ n = len - pos;
+ }
+ return StaticString(content + pos, n);
+ }
+ }
+
bool operator==(const StaticString &other) const {
return len == other.len && memcmp(content, other.content, len) == 0;
}
@@ -83,4 +83,76 @@ namespace tut {
string("hello world") < string("hello")
);
}
+
+ TEST_METHOD(4) {
+ // Test find()
+ ensure_equals("Assertion 1",
+ StaticString("").find('c'),
+ string::npos
+ );
+ ensure_equals("Assertion 2",
+ StaticString("hello world").find('c'),
+ string::npos
+ );
+ ensure_equals("Assertion 3",
+ StaticString("hello world").find('h'),
+ (string::size_type) 0
+ );
+ ensure_equals("Assertion 4",
+ StaticString("hello world").find('o'),
+ (string::size_type) 4
+ );
+
+ ensure_equals("Assertion 5",
+ StaticString("hello world").find('h', 1),
+ string::npos
+ );
+ ensure_equals("Assertion 6",
+ StaticString("hello world").find('o', 1),
+ (string::size_type) 4
+ );
+ ensure_equals("Assertion 7",
+ StaticString("hello world").find('o', 5),
+ (string::size_type) 7
+ );
+
+ ensure_equals("Assertion 8",
+ StaticString("hello world").find('h', 12),
+ string::npos
+ );
+ ensure_equals("Assertion 9",
+ StaticString("hello world").find('h', 20),
+ string::npos
+ );
+ }
+
+ TEST_METHOD(5) {
+ // Test substr()
+ ensure_equals("Assertion 1",
+ StaticString("hello world").substr(),
+ "hello world");
+ ensure_equals("Assertion 2",
+ StaticString("hello world").substr(1),
+ "ello world");
+ ensure_equals("Assertion 3",
+ StaticString("hello world").substr(4),
+ "o world");
+ ensure_equals("Assertion 4",
+ StaticString("hello world").substr(11),
+ "");
+
+ try {
+ StaticString("hello world").substr(12);
+ fail("out_of_range expected");
+ } catch (out_of_range &) {
+ // Success.
+ }
+
+ ensure_equals("Assertion 5",
+ StaticString("hello world").substr(2, 3),
+ "llo");
+ ensure_equals("Assertion 6",
+ StaticString("hello world").substr(6, 10),
+ "world");
+ }
}

0 comments on commit f79a9f7

Please sign in to comment.