Move StringHelper from Clownfish to Lucy
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/3d3e4ced Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/3d3e4ced Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/3d3e4ced Branch: refs/heads/master Commit: 3d3e4cedc5ad224c08614843edda972939baeace Parents: ed2010c Author: Nick Wellnhofer <[email protected]> Authored: Tue Aug 2 19:33:32 2016 +0200 Committer: Nick Wellnhofer <[email protected]> Committed: Tue Aug 2 19:33:32 2016 +0200 ---------------------------------------------------------------------- runtime/core/Clownfish/Util/StringHelper.c | 85 --------------- runtime/core/Clownfish/Util/StringHelper.cfh | 59 ----------- .../perl/buildlib/Clownfish/Build/Binding.pm | 101 ------------------ runtime/perl/lib/Clownfish.pm | 18 ---- runtime/perl/lib/Clownfish/Util/StringHelper.pm | 25 ----- runtime/perl/t/core/032-string_helper.t | 25 ----- runtime/test/Clownfish/Test.c | 2 - .../test/Clownfish/Test/Util/TestStringHelper.c | 106 ------------------- .../Clownfish/Test/Util/TestStringHelper.cfh | 29 ----- 9 files changed, 450 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/core/Clownfish/Util/StringHelper.c ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Util/StringHelper.c b/runtime/core/Clownfish/Util/StringHelper.c deleted file mode 100644 index 7b8e9d8..0000000 --- a/runtime/core/Clownfish/Util/StringHelper.c +++ /dev/null @@ -1,85 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define C_CFISH_STRINGHELPER -#define CFISH_USE_SHORT_NAMES - -#include "Clownfish/Util/StringHelper.h" -#include "Clownfish/CharBuf.h" -#include "Clownfish/Err.h" -#include "Clownfish/String.h" -#include "Clownfish/Util/Memory.h" - -const uint8_t cfish_StrHelp_UTF8_COUNT[] = { - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, - 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, - 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -}; - -size_t -StrHelp_overlap(const char *a, const char *b, size_t a_len, size_t b_len) { - size_t i; - const size_t len = a_len <= b_len ? a_len : b_len; - - for (i = 0; i < len; i++) { - if (*a++ != *b++) { break; } - } - return i; -} - -static const char base36_chars[] = "0123456789abcdefghijklmnopqrstuvwxyz"; - -size_t -StrHelp_to_base36(uint64_t num, void *buffer) { - char my_buf[StrHelp_MAX_BASE36_BYTES]; - char *buf = my_buf + StrHelp_MAX_BASE36_BYTES - 1; - char *end = buf; - - // Null terminate. - *buf = '\0'; - - // Convert to base 36 characters. - do { - *(--buf) = base36_chars[num % 36]; - num /= 36; - } while (num > 0); - - size_t size = (size_t)(end - buf); - memcpy(buffer, buf, size + 1); - return size; -} - -const char* -StrHelp_back_utf8_char(const char *ptr, const char *start) { - while (--ptr >= start) { - if ((*ptr & 0xC0) != 0x80) { return ptr; } - } - return NULL; -} - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/core/Clownfish/Util/StringHelper.cfh ---------------------------------------------------------------------- diff --git a/runtime/core/Clownfish/Util/StringHelper.cfh b/runtime/core/Clownfish/Util/StringHelper.cfh deleted file mode 100644 index 1e915e6..0000000 --- a/runtime/core/Clownfish/Util/StringHelper.cfh +++ /dev/null @@ -1,59 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -parcel Clownfish; - -inert class Clownfish::Util::StringHelper nickname StrHelp { - - /* A table where the values indicate the number of bytes in a UTF-8 - * sequence implied by the leading utf8 byte. - */ - inert const uint8_t[] UTF8_COUNT; - - /** Return the number of bytes that two strings have in common. - */ - inert size_t - overlap(const char *a, const char *b, size_t a_len, size_t b_len); - - /** Encode a NULL-terminated string representation of a value in base 36 - * into `buffer`. - * - * @param value The number to be encoded. - * @param buffer A buffer at least MAX_BASE36_BYTES bytes long. - * @return the number of digits encoded (not including the terminating - * NULL). - */ - inert size_t - to_base36(uint64_t value, void *buffer); - - /** Return the first non-continuation byte before the supplied pointer. - * If backtracking progresses beyond the supplied start, return NULL. - */ - inert nullable const char* - back_utf8_char(const char *utf8, const char *start); -} - -__C__ -/** The maximum number of bytes encoded by to_base36(), including the - * terminating NULL. - */ -#define cfish_StrHelp_MAX_BASE36_BYTES 14 -#ifdef CFISH_USE_SHORT_NAMES - #define StrHelp_MAX_BASE36_BYTES cfish_StrHelp_MAX_BASE36_BYTES -#endif -__END_C__ - - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/perl/buildlib/Clownfish/Build/Binding.pm ---------------------------------------------------------------------- diff --git a/runtime/perl/buildlib/Clownfish/Build/Binding.pm b/runtime/perl/buildlib/Clownfish/Build/Binding.pm index ecc83b5..9435923 100644 --- a/runtime/perl/buildlib/Clownfish/Build/Binding.pm +++ b/runtime/perl/buildlib/Clownfish/Build/Binding.pm @@ -38,7 +38,6 @@ sub bind_all { $class->bind_obj; $class->bind_vector; $class->bind_class; - $class->bind_stringhelper; } sub bind_clownfish { @@ -943,104 +942,4 @@ END_XS_CODE Clownfish::CFC::Binding::Perl::Class->register($binding); } -sub bind_stringhelper { - my $xs_code = <<'END_XS_CODE'; -MODULE = Clownfish PACKAGE = Clownfish::Util::StringHelper - -=for comment - -Turn an SV's UTF8 flag on. Equivalent to Encode::_utf8_on, but we don't have -to load Encode. - -=cut - -void -utf8_flag_on(sv) - SV *sv; -PPCODE: - SvUTF8_on(sv); - -=for comment - -Turn an SV's UTF8 flag off. - -=cut - -void -utf8_flag_off(sv) - SV *sv; -PPCODE: - SvUTF8_off(sv); - -SV* -to_base36(num) - uint64_t num; -CODE: -{ - char base36[cfish_StrHelp_MAX_BASE36_BYTES]; - size_t size = cfish_StrHelp_to_base36(num, &base36); - RETVAL = newSVpvn(base36, size); -} -OUTPUT: RETVAL - -IV -from_base36(str) - char *str; -CODE: - RETVAL = strtol(str, NULL, 36); -OUTPUT: RETVAL - -=for comment - -Upgrade a SV to UTF8, converting Latin1 if necessary. Equivalent to -utf::upgrade(). - -=cut - -void -utf8ify(sv) - SV *sv; -PPCODE: - sv_utf8_upgrade(sv); - -bool -utf8_valid(sv) - SV *sv; -CODE: -{ - STRLEN len; - char *ptr = SvPV(sv, len); - RETVAL = cfish_Str_utf8_valid(ptr, len); -} -OUTPUT: RETVAL - -=for comment - -Concatenate one scalar onto the end of the other, ignoring UTF-8 status of the -second scalar. This is necessary because $not_utf8 . $utf8 results in a -scalar which has been infected by the UTF-8 flag of the second argument. - -=cut - -void -cat_bytes(sv, catted) - SV *sv; - SV *catted; -PPCODE: -{ - STRLEN len; - char *ptr = SvPV(catted, len); - if (SvUTF8(sv)) { CFISH_THROW(CFISH_ERR, "Can't cat_bytes onto a UTF-8 SV"); } - sv_catpvn(sv, ptr, len); -} -END_XS_CODE - - my $binding = Clownfish::CFC::Binding::Perl::Class->new( - class_name => "Clownfish::Util::StringHelper", - ); - $binding->append_xs($xs_code); - - Clownfish::CFC::Binding::Perl::Class->register($binding); -} - 1; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/perl/lib/Clownfish.pm ---------------------------------------------------------------------- diff --git a/runtime/perl/lib/Clownfish.pm b/runtime/perl/lib/Clownfish.pm index c509513..7cc5f05 100644 --- a/runtime/perl/lib/Clownfish.pm +++ b/runtime/perl/lib/Clownfish.pm @@ -44,24 +44,6 @@ BEGIN { sub error {$Clownfish::Err::error} { - package Clownfish::Util::StringHelper; - our $VERSION = '0.005000'; - $VERSION = eval $VERSION; - BEGIN { - push our @ISA, 'Exporter'; - our @EXPORT_OK = qw( - utf8_flag_on - utf8_flag_off - to_base36 - from_base36 - utf8ify - utf8_valid - cat_bytes - ); - } -} - -{ package Clownfish::Obj; our $VERSION = '0.005000'; $VERSION = eval $VERSION; http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/perl/lib/Clownfish/Util/StringHelper.pm ---------------------------------------------------------------------- diff --git a/runtime/perl/lib/Clownfish/Util/StringHelper.pm b/runtime/perl/lib/Clownfish/Util/StringHelper.pm deleted file mode 100644 index b34a19f..0000000 --- a/runtime/perl/lib/Clownfish/Util/StringHelper.pm +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -package Clownfish::Util::StringHelper; -use Clownfish; -our $VERSION = '0.005000'; -$VERSION = eval $VERSION; - -1; - -__END__ - - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/perl/t/core/032-string_helper.t ---------------------------------------------------------------------- diff --git a/runtime/perl/t/core/032-string_helper.t b/runtime/perl/t/core/032-string_helper.t deleted file mode 100644 index ffa4fbe..0000000 --- a/runtime/perl/t/core/032-string_helper.t +++ /dev/null @@ -1,25 +0,0 @@ -# Licensed to the Apache Software Foundation (ASF) under one or more -# contributor license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright ownership. -# The ASF licenses this file to You under the Apache License, Version 2.0 -# (the "License"); you may not use this file except in compliance with -# the License. You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -use strict; -use warnings; - -use Clownfish::Test; -my $success = Clownfish::Test::run_tests( - "Clownfish::Test::Util::TestStringHelper" -); - -exit($success ? 0 : 1); - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/test/Clownfish/Test.c ---------------------------------------------------------------------- diff --git a/runtime/test/Clownfish/Test.c b/runtime/test/Clownfish/Test.c index 4ae6163..3e2d9e4 100644 --- a/runtime/test/Clownfish/Test.c +++ b/runtime/test/Clownfish/Test.c @@ -39,7 +39,6 @@ #include "Clownfish/Test/TestVector.h" #include "Clownfish/Test/Util/TestAtomic.h" #include "Clownfish/Test/Util/TestMemory.h" -#include "Clownfish/Test/Util/TestStringHelper.h" TestSuite* Test_create_test_suite() { @@ -58,7 +57,6 @@ Test_create_test_suite() { TestSuite_Add_Batch(suite, (TestBatch*)TestCB_new()); TestSuite_Add_Batch(suite, (TestBatch*)TestBoolean_new()); TestSuite_Add_Batch(suite, (TestBatch*)TestNum_new()); - TestSuite_Add_Batch(suite, (TestBatch*)TestStrHelp_new()); TestSuite_Add_Batch(suite, (TestBatch*)TestAtomic_new()); TestSuite_Add_Batch(suite, (TestBatch*)TestLFReg_new()); TestSuite_Add_Batch(suite, (TestBatch*)TestMemory_new()); http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/test/Clownfish/Test/Util/TestStringHelper.c ---------------------------------------------------------------------- diff --git a/runtime/test/Clownfish/Test/Util/TestStringHelper.c b/runtime/test/Clownfish/Test/Util/TestStringHelper.c deleted file mode 100644 index 2caee84..0000000 --- a/runtime/test/Clownfish/Test/Util/TestStringHelper.c +++ /dev/null @@ -1,106 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include <string.h> - -#define CFISH_USE_SHORT_NAMES -#define TESTCFISH_USE_SHORT_NAMES - -#include "Clownfish/Test/Util/TestStringHelper.h" - -#include "Clownfish/String.h" -#include "Clownfish/Err.h" -#include "Clownfish/Test.h" -#include "Clownfish/TestHarness/TestBatchRunner.h" -#include "Clownfish/Util/StringHelper.h" -#include "Clownfish/Class.h" - -TestStringHelper* -TestStrHelp_new() { - return (TestStringHelper*)Class_Make_Obj(TESTSTRINGHELPER); -} - -static void -test_overlap(TestBatchRunner *runner) { - size_t result; - result = StrHelp_overlap("", "", 0, 0); - TEST_UINT_EQ(runner, result, 0, "two empty strings"); - result = StrHelp_overlap("", "foo", 0, 3); - TEST_UINT_EQ(runner, result, 0, "first string is empty"); - result = StrHelp_overlap("foo", "", 3, 0); - TEST_UINT_EQ(runner, result, 0, "second string is empty"); - result = StrHelp_overlap("foo", "foo", 3, 3); - TEST_UINT_EQ(runner, result, 3, "equal strings"); - result = StrHelp_overlap("foo bar", "foo", 7, 3); - TEST_UINT_EQ(runner, result, 3, "first string is longer"); - result = StrHelp_overlap("foo", "foo bar", 3, 7); - TEST_UINT_EQ(runner, result, 3, "second string is longer"); - result = StrHelp_overlap("bar", "baz", 3, 3); - TEST_UINT_EQ(runner, result, 2, "different byte"); -} - - -static void -test_to_base36(TestBatchRunner *runner) { - char buffer[StrHelp_MAX_BASE36_BYTES]; - StrHelp_to_base36(UINT64_MAX, buffer); - TEST_STR_EQ(runner, "3w5e11264sgsf", buffer, "base36 UINT64_MAX"); - StrHelp_to_base36(1, buffer); - TEST_STR_EQ(runner, "1", buffer, "base36 1"); - TEST_INT_EQ(runner, buffer[1], 0, "base36 NULL termination"); -} - -static void -test_back_utf8_char(TestBatchRunner *runner) { - char buffer[4]; - char *buf = buffer + 1; - uint32_t len = Str_encode_utf8_char(0x263A, buffer); - char *end = buffer + len; - TEST_TRUE(runner, StrHelp_back_utf8_char(end, buffer) == buffer, - "back_utf8_char"); - TEST_TRUE(runner, StrHelp_back_utf8_char(end, buf) == NULL, - "back_utf8_char returns NULL rather than back up beyond start"); - TEST_TRUE(runner, StrHelp_back_utf8_char(buffer, buffer) == NULL, - "back_utf8_char returns NULL when end == start"); - - int32_t code_point; - for (code_point = 0; code_point <= 0x10FFFF; code_point++) { - uint32_t size = Str_encode_utf8_char(code_point, buffer); - char *start = buffer; - char *end = start + size; - - if (StrHelp_back_utf8_char(end, start) != start) { - break; - } - } - if (code_point == 0x110000) { - PASS(runner, "back_utf8_char works for code points 0 - 0x10FFFF"); - } - else { - FAIL(runner, "Failed back_utf8_char at 0x%.1X", (unsigned)code_point); - } -} - -void -TestStrHelp_Run_IMP(TestStringHelper *self, TestBatchRunner *runner) { - TestBatchRunner_Plan(runner, (TestBatch*)self, 14); - test_overlap(runner); - test_to_base36(runner); - test_back_utf8_char(runner); -} - - - http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/3d3e4ced/runtime/test/Clownfish/Test/Util/TestStringHelper.cfh ---------------------------------------------------------------------- diff --git a/runtime/test/Clownfish/Test/Util/TestStringHelper.cfh b/runtime/test/Clownfish/Test/Util/TestStringHelper.cfh deleted file mode 100644 index 752c553..0000000 --- a/runtime/test/Clownfish/Test/Util/TestStringHelper.cfh +++ /dev/null @@ -1,29 +0,0 @@ -/* Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -parcel TestClownfish; - -class Clownfish::Test::Util::TestStringHelper nickname TestStrHelp - inherits Clownfish::TestHarness::TestBatch { - - inert incremented TestStringHelper* - new(); - - void - Run(TestStringHelper *self, TestBatchRunner *runner); -} - -
