Skip to content

Commit

Permalink
[string] string.ToLower, string.ToUpper in C++ using ICU.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Feb 19, 2024
1 parent 0c75701 commit 82c353d
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 77 deletions.
27 changes: 11 additions & 16 deletions GenCpp.fu
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,15 @@ public class GenCpp : GenCCpp
WriteArgsInParentheses(method, args);
}

void WriteStringToLowerUpper!(FuExpr obj, string name)
{
Include("string");
Include("unicode/unistr.h");
Write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to");
Write(name);
WriteCall("er().toUTF8String(result); }", obj);
}

void WriteAllAnyContains!(string function, FuExpr obj, List<FuExpr#> args)
{
Include("algorithm");
Expand Down Expand Up @@ -809,24 +818,10 @@ public class GenCpp : GenCCpp
WriteStringMethod(obj, "substr", method, args);
break;
case FuId.StringToLower:
Include("algorithm");
Include("cctype");
Write("[&] { std::string data = std::string{");
obj.Accept(this, FuPriority.Argument);
Write("}; ");
Write("std::transform(data.begin(), data.end(), data.begin(), ");
Write("[](unsigned char c) { return std::tolower(c); }); ");
Write("return data; }()");
WriteStringToLowerUpper(obj, "Low");
break;
case FuId.StringToUpper:
Include("algorithm");
Include("cctype");
Write("[&] { std::string data = std::string{");
obj.Accept(this, FuPriority.Argument);
Write("}; ");
Write("std::transform(data.begin(), data.end(), data.begin(), ");
Write("[](unsigned char c) { return std::toupper(c); }); ");
Write("return data; }()");
WriteStringToLowerUpper(obj, "Upp");
break;
case FuId.ArrayBinarySearchAll:
case FuId.ArrayBinarySearchPart:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ test/bin/%/c.exe: test/bin/%/Test.c
$(DO)$(CC) -o $@ $(TEST_CFLAGS) -Wno-unused-function -I $(<D) $^ `pkg-config --cflags --libs glib-2.0` -lm || grep '//FAIL:.*\<c\>' test/$*.fu

test/bin/%/cpp.exe: test/bin/%/Test.cpp
$(DO)$(CXX) -o $@ $(TEST_CXXFLAGS) -I $(<D) $^ || grep '//FAIL:.*\<cpp\>' test/$*.fu
$(DO)$(CXX) -o $@ $(TEST_CXXFLAGS) -I $(<D) $^ $(if $(findstring $*, StringToLower StringToUpper), -licuuc) || grep '//FAIL:.*\<cpp\>' test/$*.fu

test/bin/%/cs.dll: test/bin/%/Test.cs
$(DO)$(CSC) -out:$@ $^ || grep '//FAIL:.*\<cs\>' test/$*.fu
Expand Down
27 changes: 11 additions & 16 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13532,6 +13532,15 @@ void GenCpp::writeStringMethod(const FuExpr * obj, std::string_view name, const
writeArgsInParentheses(method, args);
}

void GenCpp::writeStringToLowerUpper(const FuExpr * obj, std::string_view name)
{
include("string");
include("unicode/unistr.h");
write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to");
write(name);
writeCall("er().toUTF8String(result); }", obj);
}

void GenCpp::writeAllAnyContains(std::string_view function, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args)
{
include("algorithm");
Expand Down Expand Up @@ -13770,24 +13779,10 @@ void GenCpp::writeCallExpr(const FuExpr * obj, const FuMethod * method, const st
writeStringMethod(obj, "substr", method, args);
break;
case FuId::stringToLower:
include("algorithm");
include("cctype");
write("[&] { std::string data = std::string{");
obj->accept(this, FuPriority::argument);
write("}; ");
write("std::transform(data.begin(), data.end(), data.begin(), ");
write("[](unsigned char c) { return std::tolower(c); }); ");
write("return data; }()");
writeStringToLowerUpper(obj, "Low");
break;
case FuId::stringToUpper:
include("algorithm");
include("cctype");
write("[&] { std::string data = std::string{");
obj->accept(this, FuPriority::argument);
write("}; ");
write("std::transform(data.begin(), data.end(), data.begin(), ");
write("[](unsigned char c) { return std::toupper(c); }); ");
write("return data; }()");
writeStringToLowerUpper(obj, "Upp");
break;
case FuId::arrayBinarySearchAll:
case FuId::arrayBinarySearchPart:
Expand Down
27 changes: 11 additions & 16 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13882,6 +13882,15 @@ void WriteStringMethod(FuExpr obj, string name, FuMethod method, List<FuExpr> ar
WriteArgsInParentheses(method, args);
}

void WriteStringToLowerUpper(FuExpr obj, string name)
{
Include("string");
Include("unicode/unistr.h");
Write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to");
Write(name);
WriteCall("er().toUTF8String(result); }", obj);
}

void WriteAllAnyContains(string function, FuExpr obj, List<FuExpr> args)
{
Include("algorithm");
Expand Down Expand Up @@ -14119,24 +14128,10 @@ protected override void WriteCallExpr(FuExpr obj, FuMethod method, List<FuExpr>
WriteStringMethod(obj, "substr", method, args);
break;
case FuId.StringToLower:
Include("algorithm");
Include("cctype");
Write("[&] { std::string data = std::string{");
obj.Accept(this, FuPriority.Argument);
Write("}; ");
Write("std::transform(data.begin(), data.end(), data.begin(), ");
Write("[](unsigned char c) { return std::tolower(c); }); ");
Write("return data; }()");
WriteStringToLowerUpper(obj, "Low");
break;
case FuId.StringToUpper:
Include("algorithm");
Include("cctype");
Write("[&] { std::string data = std::string{");
obj.Accept(this, FuPriority.Argument);
Write("}; ");
Write("std::transform(data.begin(), data.end(), data.begin(), ");
Write("[](unsigned char c) { return std::toupper(c); }); ");
Write("return data; }()");
WriteStringToLowerUpper(obj, "Upp");
break;
case FuId.ArrayBinarySearchAll:
case FuId.ArrayBinarySearchPart:
Expand Down
1 change: 1 addition & 0 deletions libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,7 @@ class GenCpp : public GenCCpp
void writePtrRange(const FuExpr * obj, const FuExpr * index, const FuExpr * count);
void writeNotRawStringLiteral(const FuExpr * obj, FuPriority priority);
void writeStringMethod(const FuExpr * obj, std::string_view name, const FuMethod * method, const std::vector<std::shared_ptr<FuExpr>> * args);
void writeStringToLowerUpper(const FuExpr * obj, std::string_view name);
void writeAllAnyContains(std::string_view function, const FuExpr * obj, const std::vector<std::shared_ptr<FuExpr>> * args);
void writeCollectionMethod(const FuExpr * obj, std::string_view name, const std::vector<std::shared_ptr<FuExpr>> * args);
void writeCString(const FuExpr * expr);
Expand Down
27 changes: 11 additions & 16 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -14312,6 +14312,15 @@ export class GenCpp extends GenCCpp
this.writeArgsInParentheses(method, args);
}

#writeStringToLowerUpper(obj, name)
{
this.include("string");
this.include("unicode/unistr.h");
this.write("[](icu::StringPiece s) { std::string result; return icu::UnicodeString::fromUTF8(s).to");
this.write(name);
this.writeCall("er().toUTF8String(result); }", obj);
}

#writeAllAnyContains(function_, obj, args)
{
this.include("algorithm");
Expand Down Expand Up @@ -14551,24 +14560,10 @@ export class GenCpp extends GenCCpp
this.#writeStringMethod(obj, "substr", method, args);
break;
case FuId.STRING_TO_LOWER:
this.include("algorithm");
this.include("cctype");
this.write("[&] { std::string data = std::string{");
obj.accept(this, FuPriority.ARGUMENT);
this.write("}; ");
this.write("std::transform(data.begin(), data.end(), data.begin(), ");
this.write("[](unsigned char c) { return std::tolower(c); }); ");
this.write("return data; }()");
this.#writeStringToLowerUpper(obj, "Low");
break;
case FuId.STRING_TO_UPPER:
this.include("algorithm");
this.include("cctype");
this.write("[&] { std::string data = std::string{");
obj.accept(this, FuPriority.ARGUMENT);
this.write("}; ");
this.write("std::transform(data.begin(), data.end(), data.begin(), ");
this.write("[](unsigned char c) { return std::toupper(c); }); ");
this.write("return data; }()");
this.#writeStringToLowerUpper(obj, "Upp");
break;
case FuId.ARRAY_BINARY_SEARCH_ALL:
case FuId.ARRAY_BINARY_SEARCH_PART:
Expand Down
12 changes: 6 additions & 6 deletions test/StringToLower.fu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://pastebin.com/fuw4Uizk
// https://pastebin.com/fuw4Uizk
// ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ ΟΔΥΣΣΕΥΣ
// đâăêôơưấắếốớứầằềồờừậặệộợựш ὀδυσσεύς

Expand All @@ -8,11 +8,11 @@ public class Test
{
string() orig1 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; //FAIL: cl
string orig2 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш";
string() new1 = orig1.ToLower();
string() new1 = orig1.ToLower(); //FAIL: cpp requires ICU
string() new2 = orig2.ToLower();
return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" &&
orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" &&
new1 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш" &&
new2 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш";
return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"
&& orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"
&& new1 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш"
&& new2 == "mixed case đâăêôơưấắếốớứầằềồờừậặệộợựш";
}
}
12 changes: 6 additions & 6 deletions test/StringToUpper.fu
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// https://pastebin.com/fuw4Uizk
// https://pastebin.com/fuw4Uizk
// ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ
// đâăêôơưấắếốớứầằềồờừậặệộợựш

Expand All @@ -8,11 +8,11 @@ public class Test
{
string() orig1 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"; //FAIL: cl
string orig2 = "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш";
string() new1 = orig1.ToUpper();
string() new1 = orig1.ToUpper(); //FAIL: cpp requires ICU
string() new2 = orig2.ToUpper();
return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" &&
orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш" &&
new1 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ" &&
new2 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ";
return orig1 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"
&& orig2 == "MiXeD cAsE ĐÂĂÊÔơưấắếỐỚỨẦẰềồờừậẶỆỘỢỰш"
&& new1 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ"
&& new2 == "MIXED CASE ĐÂĂÊÔƠƯẤẮẾỐỚỨẦẰỀỒỜỪẬẶỆỘỢỰШ";
}
}

0 comments on commit 82c353d

Please sign in to comment.