Skip to content

Commit 99c95da

Browse files
committed
[cleanup] Refactor string.Substring.
1 parent 9107a5c commit 99c95da

File tree

8 files changed

+126
-145
lines changed

8 files changed

+126
-145
lines changed

GenC.fu

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,15 @@ public class GenC : GenCCpp
118118
WriteTemporaryOrExpr(expr, FuPriority.Argument);
119119
}
120120

121-
void WriteStringPtrAddCast!(FuCallExpr call)
121+
void WriteStringPtrAdd!(FuExpr? obj, bool utf8GetString, List<FuExpr#> args, bool cast)
122122
{
123-
if (IsUTF8GetString(call))
124-
Write("(const char *) ");
125-
WriteStringPtrAdd(call);
123+
if (utf8GetString) {
124+
if (cast)
125+
Write("(const char *) ");
126+
WriteArrayPtrAdd(args[0], args[1]);
127+
}
128+
else
129+
WriteArrayPtrAdd(obj, args[0]);
126130
}
127131

128132
static bool IsDictionaryClassStgIndexing(FuExpr expr)
@@ -180,7 +184,7 @@ public class GenC : GenCCpp
180184
if (call != null) {
181185
GetStringSubstringLength(call).Accept(this, FuPriority.Argument);
182186
Write(", ");
183-
WriteStringPtrAddCast(call);
187+
WriteStringPtrAdd(call.Method.Left, IsUTF8GetString(call), call.Arguments, true);
184188
}
185189
else if (expr.Type is FuClassType klass && klass.Class.Id != FuId.StringClass) {
186190
// TODO: abstract, virtual, override
@@ -773,14 +777,16 @@ public class GenC : GenCCpp
773777
WriteChar(')');
774778
}
775779

780+
static FuExpr GetStringSubstringLength(FuCallExpr call) => call.Arguments[IsUTF8GetString(call) ? 2 : 1];
781+
776782
void WriteStringStorageValue!(FuExpr expr)
777783
{
778784
FuCallExpr? call = IsStringSubstring(expr);
779785
if (call != null) {
780786
Include("string.h");
781787
this.StringSubstring = true;
782788
Write("FuString_Substring(");
783-
WriteStringPtrAddCast(call);
789+
WriteStringPtrAdd(call.Method.Left, IsUTF8GetString(call), call.Arguments, true);
784790
Write(", ");
785791
GetStringSubstringLength(call).Accept(this, FuPriority.Argument);
786792
WriteChar(')');
@@ -1572,7 +1578,7 @@ public class GenC : GenCCpp
15721578
WriteChar('(');
15731579
Include("string.h");
15741580
Write("memcmp(");
1575-
WriteStringPtrAdd(call);
1581+
WriteStringPtrAdd(call.Method.Left, IsUTF8GetString(call), call.Arguments, false);
15761582
Write(", ");
15771583
VisitLiteralString(literal);
15781584
Write(", ");

GenCCpp.fu

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GenCCpp.fu - C/C++ code generator
22
//
3-
// Copyright (C) 2011-2023 Piotr Fusik
3+
// Copyright (C) 2011-2024 Piotr Fusik
44
//
55
// This file is part of Fusion Transpiler,
66
// see https://github.com/fusionlanguage/fut
@@ -154,25 +154,14 @@ public abstract class GenCCpp : GenCCppD
154154

155155
protected static bool IsUTF8GetString(FuCallExpr call) => call.Method.Symbol.Id == FuId.UTF8GetString;
156156

157-
protected static FuExpr GetStringSubstringPtr(FuCallExpr call) => IsUTF8GetString(call) ? call.Arguments[0] : call.Method.Left;
158-
159-
protected static FuExpr GetStringSubstringOffset(FuCallExpr call) => call.Arguments[IsUTF8GetString(call) ? 1 : 0];
160-
161-
protected static FuExpr GetStringSubstringLength(FuCallExpr call) => call.Arguments[IsUTF8GetString(call) ? 2 : 1];
162-
163-
protected void WriteStringPtrAdd!(FuCallExpr call)
164-
{
165-
WriteArrayPtrAdd(GetStringSubstringPtr(call), GetStringSubstringOffset(call));
166-
}
167-
168157
protected static FuExpr? IsTrimSubstring(FuBinaryExpr expr)
169158
{
170159
FuCallExpr? call = IsStringSubstring(expr.Right);
171160
if (call != null
172161
&& !IsUTF8GetString(call)
173-
&& expr.Left is FuSymbolReference leftSymbol && GetStringSubstringPtr(call).IsReferenceTo(leftSymbol.Symbol) // TODO: more complex expr
174-
&& GetStringSubstringOffset(call).IsLiteralZero())
175-
return GetStringSubstringLength(call);
162+
&& expr.Left is FuSymbolReference leftSymbol && call.Method.Left.IsReferenceTo(leftSymbol.Symbol) // TODO: more complex expr
163+
&& call.Arguments[0].IsLiteralZero())
164+
return call.Arguments[1];
176165
return null;
177166
}
178167

GenCl.fu

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// GenCl.fu - OpenCL C code generator
22
//
3-
// Copyright (C) 2020-2023 Piotr Fusik
3+
// Copyright (C) 2020-2024 Piotr Fusik
44
//
55
// This file is part of Fusion Transpiler,
66
// see https://github.com/fusionlanguage/fut
@@ -133,12 +133,13 @@ public class GenCl : GenC
133133
if (IsUTF8GetString(call)) {
134134
this.BytesEqualsString = true;
135135
Write("FuBytes_Equals(");
136+
WriteArrayPtrAdd(call.Arguments[0], call.Arguments[1]);
136137
}
137138
else {
138139
this.StringStartsWith = true;
139140
Write("FuString_StartsWith(");
141+
WriteArrayPtrAdd(call.Method.Left, call.Arguments[0]);
140142
}
141-
WriteStringPtrAdd(call);
142143
Write(", ");
143144
VisitLiteralString(literal);
144145
WriteChar(')');

GenCpp.fu

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,16 +1578,19 @@ public class GenCpp : GenCCpp
15781578
FuCallExpr? call = IsStringSubstring(expr);
15791579
if (call != null
15801580
&& type.Id == FuId.StringStorageType
1581-
&& GetStringSubstringPtr(call).Type.Id != FuId.StringStorageType) {
1581+
&& (IsUTF8GetString(call) ? call.Arguments[0] : call.Method.Left).Type.Id != FuId.StringStorageType) {
15821582
Write("std::string(");
1583-
bool cast = IsUTF8GetString(call);
1584-
if (cast)
1583+
if (IsUTF8GetString(call)) {
15851584
Write("reinterpret_cast<const char *>(");
1586-
WriteStringPtrAdd(call);
1587-
if (cast)
1588-
WriteChar(')');
1589-
Write(", ");
1590-
GetStringSubstringLength(call).Accept(this, FuPriority.Argument);
1585+
WriteArrayPtrAdd(call.Arguments[0], call.Arguments[1]);
1586+
Write("), ");
1587+
call.Arguments[2].Accept(this, FuPriority.Argument);
1588+
}
1589+
else {
1590+
WriteArrayPtrAdd(call.Method.Left, call.Arguments[0]);
1591+
Write(", ");
1592+
call.Arguments[1].Accept(this, FuPriority.Argument);
1593+
}
15911594
WriteChar(')');
15921595
}
15931596
else

libfut.cpp

Lines changed: 31 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8913,32 +8913,12 @@ bool GenCCpp::isUTF8GetString(const FuCallExpr * call)
89138913
return call->method->symbol->id == FuId::uTF8GetString;
89148914
}
89158915

8916-
const FuExpr * GenCCpp::getStringSubstringPtr(const FuCallExpr * call)
8917-
{
8918-
return isUTF8GetString(call) ? call->arguments[0].get() : call->method->left.get();
8919-
}
8920-
8921-
const FuExpr * GenCCpp::getStringSubstringOffset(const FuCallExpr * call)
8922-
{
8923-
return call->arguments[isUTF8GetString(call) ? 1 : 0].get();
8924-
}
8925-
8926-
const FuExpr * GenCCpp::getStringSubstringLength(const FuCallExpr * call)
8927-
{
8928-
return call->arguments[isUTF8GetString(call) ? 2 : 1].get();
8929-
}
8930-
8931-
void GenCCpp::writeStringPtrAdd(const FuCallExpr * call)
8932-
{
8933-
writeArrayPtrAdd(getStringSubstringPtr(call), getStringSubstringOffset(call));
8934-
}
8935-
89368916
const FuExpr * GenCCpp::isTrimSubstring(const FuBinaryExpr * expr)
89378917
{
89388918
const FuCallExpr * call = isStringSubstring(expr->right.get());
89398919
const FuSymbolReference * leftSymbol;
8940-
if (call != nullptr && !isUTF8GetString(call) && (leftSymbol = dynamic_cast<const FuSymbolReference *>(expr->left.get())) && getStringSubstringPtr(call)->isReferenceTo(leftSymbol->symbol) && getStringSubstringOffset(call)->isLiteralZero())
8941-
return getStringSubstringLength(call);
8920+
if (call != nullptr && !isUTF8GetString(call) && (leftSymbol = dynamic_cast<const FuSymbolReference *>(expr->left.get())) && call->method->left->isReferenceTo(leftSymbol->symbol) && call->arguments[0]->isLiteralZero())
8921+
return call->arguments[1].get();
89428922
return nullptr;
89438923
}
89448924

@@ -9139,11 +9119,15 @@ void GenC::writeInterpolatedStringArgBase(const FuExpr * expr)
91399119
writeTemporaryOrExpr(expr, FuPriority::argument);
91409120
}
91419121

9142-
void GenC::writeStringPtrAddCast(const FuCallExpr * call)
9122+
void GenC::writeStringPtrAdd(const FuExpr * obj, bool utf8GetString, const std::vector<std::shared_ptr<FuExpr>> * args, bool cast)
91439123
{
9144-
if (isUTF8GetString(call))
9145-
write("(const char *) ");
9146-
writeStringPtrAdd(call);
9124+
if (utf8GetString) {
9125+
if (cast)
9126+
write("(const char *) ");
9127+
writeArrayPtrAdd((*args)[0].get(), (*args)[1].get());
9128+
}
9129+
else
9130+
writeArrayPtrAdd(obj, (*args)[0].get());
91479131
}
91489132

91499133
bool GenC::isDictionaryClassStgIndexing(const FuExpr * expr)
@@ -9197,7 +9181,7 @@ void GenC::writeInterpolatedStringArg(const FuExpr * expr)
91979181
if (call != nullptr) {
91989182
getStringSubstringLength(call)->accept(this, FuPriority::argument);
91999183
write(", ");
9200-
writeStringPtrAddCast(call);
9184+
writeStringPtrAdd(call->method->left.get(), isUTF8GetString(call), &call->arguments, true);
92019185
}
92029186
else {
92039187
const FuClassType * klass;
@@ -9732,14 +9716,19 @@ void GenC::writeNewArray(const FuType * elementType, const FuExpr * lengthExpr,
97329716
writeChar(')');
97339717
}
97349718

9719+
const FuExpr * GenC::getStringSubstringLength(const FuCallExpr * call)
9720+
{
9721+
return call->arguments[isUTF8GetString(call) ? 2 : 1].get();
9722+
}
9723+
97359724
void GenC::writeStringStorageValue(const FuExpr * expr)
97369725
{
97379726
const FuCallExpr * call = isStringSubstring(expr);
97389727
if (call != nullptr) {
97399728
include("string.h");
97409729
this->stringSubstring = true;
97419730
write("FuString_Substring(");
9742-
writeStringPtrAddCast(call);
9731+
writeStringPtrAdd(call->method->left.get(), isUTF8GetString(call), &call->arguments, true);
97439732
write(", ");
97449733
getStringSubstringLength(call)->accept(this, FuPriority::argument);
97459734
writeChar(')');
@@ -10512,7 +10501,7 @@ void GenC::writeSubstringEqual(const FuCallExpr * call, std::string_view literal
1051210501
writeChar('(');
1051310502
include("string.h");
1051410503
write("memcmp(");
10515-
writeStringPtrAdd(call);
10504+
writeStringPtrAdd(call->method->left.get(), isUTF8GetString(call), &call->arguments, false);
1051610505
write(", ");
1051710506
visitLiteralString(literal);
1051810507
write(", ");
@@ -12856,12 +12845,13 @@ void GenCl::writeSubstringEqual(const FuCallExpr * call, std::string_view litera
1285612845
if (isUTF8GetString(call)) {
1285712846
this->bytesEqualsString = true;
1285812847
write("FuBytes_Equals(");
12848+
writeArrayPtrAdd(call->arguments[0].get(), call->arguments[1].get());
1285912849
}
1286012850
else {
1286112851
this->stringStartsWith = true;
1286212852
write("FuString_StartsWith(");
12853+
writeArrayPtrAdd(call->method->left.get(), call->arguments[0].get());
1286312854
}
12864-
writeStringPtrAdd(call);
1286512855
write(", ");
1286612856
visitLiteralString(literal);
1286712857
writeChar(')');
@@ -14544,16 +14534,19 @@ void GenCpp::writeStronglyCoerced(const FuType * type, const FuExpr * expr)
1454414534
}
1454514535
else {
1454614536
const FuCallExpr * call = isStringSubstring(expr);
14547-
if (call != nullptr && type->id == FuId::stringStorageType && getStringSubstringPtr(call)->type->id != FuId::stringStorageType) {
14537+
if (call != nullptr && type->id == FuId::stringStorageType && (isUTF8GetString(call) ? call->arguments[0] : call->method->left)->type->id != FuId::stringStorageType) {
1454814538
write("std::string(");
14549-
bool cast = isUTF8GetString(call);
14550-
if (cast)
14539+
if (isUTF8GetString(call)) {
1455114540
write("reinterpret_cast<const char *>(");
14552-
writeStringPtrAdd(call);
14553-
if (cast)
14554-
writeChar(')');
14555-
write(", ");
14556-
getStringSubstringLength(call)->accept(this, FuPriority::argument);
14541+
writeArrayPtrAdd(call->arguments[0].get(), call->arguments[1].get());
14542+
write("), ");
14543+
call->arguments[2]->accept(this, FuPriority::argument);
14544+
}
14545+
else {
14546+
writeArrayPtrAdd(call->method->left.get(), call->arguments[0].get());
14547+
write(", ");
14548+
call->arguments[1]->accept(this, FuPriority::argument);
14549+
}
1455714550
writeChar(')');
1455814551
}
1455914552
else

0 commit comments

Comments
 (0)