You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by nw...@apache.org on 2013/09/14 21:29:34 UTC
[lucy-commits] [02/15] git commit: refs/heads/cfish-string-prep1 - Convert
Str_Swap_Chars to immutable strings
Convert Str_Swap_Chars to immutable strings
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/9dbac05f
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/9dbac05f
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/9dbac05f
Branch: refs/heads/cfish-string-prep1
Commit: 9dbac05fa438886ae5fa24e4fe062735158f3499
Parents: 8ab0675
Author: Nick Wellnhofer <we...@aevum.de>
Authored: Sat Sep 14 14:43:40 2013 +0200
Committer: Nick Wellnhofer <we...@aevum.de>
Committed: Sat Sep 14 14:43:40 2013 +0200
----------------------------------------------------------------------
clownfish/runtime/core/Clownfish/String.c | 28 +++++++-------------
clownfish/runtime/core/Clownfish/String.cfh | 7 ++---
.../runtime/core/Clownfish/Test/TestString.c | 21 ++++++++++++---
core/Lucy/Store/FSFolder.c | 9 +++++--
4 files changed, 39 insertions(+), 26 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/lucy/blob/9dbac05f/clownfish/runtime/core/Clownfish/String.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.c b/clownfish/runtime/core/Clownfish/String.c
index ef98320..d39f271 100644
--- a/clownfish/runtime/core/Clownfish/String.c
+++ b/clownfish/runtime/core/Clownfish/String.c
@@ -161,28 +161,20 @@ Str_To_String_IMP(String *self) {
return Str_new_from_trusted_utf8(self->ptr, self->size);
}
-int32_t
+String*
Str_Swap_Chars_IMP(String *self, uint32_t match, uint32_t replacement) {
- int32_t num_swapped = 0;
+ CharBuf *charbuf = CB_new(self->size);
+ StackStringIterator *iter = STR_STACKTOP(self);
+ uint32_t code_point;
- if (match > 127) {
- THROW(ERR, "match point too high: %u32", match);
- }
- else if (replacement > 127) {
- THROW(ERR, "replacement code point too high: %u32", replacement);
- }
- else {
- char *ptr = self->ptr;
- char *const limit = ptr + self->size;
- for (; ptr < limit; ptr++) {
- if (*ptr == (char)match) {
- *ptr = (char)replacement;
- num_swapped++;
- }
- }
+ while (STRITER_DONE != (code_point = SStrIter_Next(iter))) {
+ if (code_point == match) { code_point = replacement; }
+ CB_Cat_Char(charbuf, code_point);
}
- return num_swapped;
+ String *retval = CB_Yield_String(charbuf);
+ DECREF(charbuf);
+ return retval;
}
int64_t
http://git-wip-us.apache.org/repos/asf/lucy/blob/9dbac05f/clownfish/runtime/core/Clownfish/String.cfh
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/String.cfh b/clownfish/runtime/core/Clownfish/String.cfh
index f49ddd2..ee78652 100644
--- a/clownfish/runtime/core/Clownfish/String.cfh
+++ b/clownfish/runtime/core/Clownfish/String.cfh
@@ -105,10 +105,11 @@ class Clownfish::String cnick Str
incremented String*
Cat_Trusted_UTF8(String *self, const char *ptr, size_t size);
- /** Replace all instances of one character for the other. For now, both
- * the source and replacement code points must be ASCII.
+ /** Replace all instances of one character for the other.
+ *
+ * @return a new String with the characters replaced.
*/
- int32_t
+ incremented String*
Swap_Chars(String *self, uint32_t match, uint32_t replacement);
public int64_t
http://git-wip-us.apache.org/repos/asf/lucy/blob/9dbac05f/clownfish/runtime/core/Clownfish/Test/TestString.c
----------------------------------------------------------------------
diff --git a/clownfish/runtime/core/Clownfish/Test/TestString.c b/clownfish/runtime/core/Clownfish/Test/TestString.c
index 31143fc..daecdc6 100644
--- a/clownfish/runtime/core/Clownfish/Test/TestString.c
+++ b/clownfish/runtime/core/Clownfish/Test/TestString.c
@@ -35,6 +35,7 @@
static char smiley[] = { (char)0xE2, (char)0x98, (char)0xBA, 0 };
static uint32_t smiley_len = 3;
+static uint32_t smiley_cp = 0x263A;
TestString*
TestStr_new() {
@@ -60,7 +61,7 @@ S_smiley_with_whitespace(int *num_spaces_ptr) {
CharBuf *buf = CB_new(0);
for (int i = 0; i < num_spaces; i++) { CB_Cat_Char(buf, spaces[i]); }
- CB_Cat_Char(buf, 0x263A);
+ CB_Cat_Char(buf, smiley_cp);
for (int i = 0; i < num_spaces; i++) { CB_Cat_Char(buf, spaces[i]); }
String *retval = CB_To_String(buf);
@@ -138,7 +139,9 @@ test_Find(TestBatchRunner *runner) {
static void
test_Code_Point_At_and_From(TestBatchRunner *runner) {
- uint32_t code_points[] = { 'a', 0x263A, 0x263A, 'b', 0x263A, 'c' };
+ uint32_t code_points[] = {
+ 'a', smiley_cp, smiley_cp, 'b', smiley_cp, 'c'
+ };
uint32_t num_code_points = sizeof(code_points) / sizeof(uint32_t);
String *string = Str_newf("a%s%sb%sc", smiley, smiley, smiley);
uint32_t i;
@@ -289,6 +292,17 @@ test_Compare_To(TestBatchRunner *runner) {
}
static void
+test_Swap_Chars(TestBatchRunner *runner) {
+ String *source = S_get_str("aXXbXc");
+ String *got = Str_Swap_Chars(source, 'X', smiley_cp);
+ String *wanted = Str_newf("a%s%sb%sc", smiley, smiley, smiley);
+ TEST_TRUE(runner, Str_Equals(got, (Obj*)wanted), "Swap_Chars");
+ DECREF(wanted);
+ DECREF(got);
+ DECREF(source);
+}
+
+static void
test_iterator(TestBatchRunner *runner) {
static const uint32_t code_points[] = {
0x41,
@@ -481,7 +495,7 @@ test_iterator_substring(TestBatchRunner *runner) {
void
TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
- TestBatchRunner_Plan(runner, (TestBatch*)self, 99);
+ TestBatchRunner_Plan(runner, (TestBatch*)self, 100);
test_Cat(runner);
test_Clone(runner);
test_Code_Point_At_and_From(runner);
@@ -492,6 +506,7 @@ TestStr_Run_IMP(TestString *self, TestBatchRunner *runner) {
test_To_I64(runner);
test_Length(runner);
test_Compare_To(runner);
+ test_Swap_Chars(runner);
test_iterator(runner);
test_iterator_whitespace(runner);
test_iterator_substring(runner);
http://git-wip-us.apache.org/repos/asf/lucy/blob/9dbac05f/core/Lucy/Store/FSFolder.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Store/FSFolder.c b/core/Lucy/Store/FSFolder.c
index e923d33..cdb353c 100644
--- a/core/Lucy/Store/FSFolder.c
+++ b/core/Lucy/Store/FSFolder.c
@@ -270,10 +270,15 @@ static String*
S_fullpath(FSFolder *self, const String *path) {
FSFolderIVARS *const ivars = FSFolder_IVARS(self);
String *fullpath = Str_newf("%o%s%o", ivars->path, DIR_SEP, path);
+ String *retval;
if (DIR_SEP[0] != '/') {
- Str_Swap_Chars(fullpath, '/', DIR_SEP[0]);
+ retval = Str_Swap_Chars(fullpath, '/', DIR_SEP[0]);
+ DECREF(fullpath);
+ }
+ else {
+ retval = fullpath;
}
- return fullpath;
+ return retval;
}
static bool