Skip to content

Commit

Permalink
[c] Refactor integer TryParse.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Dec 10, 2024
1 parent e54e31f commit 3a7a1ab
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 111 deletions.
52 changes: 25 additions & 27 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class GenC : GenCCpp
SortedSet<FuId>() IntFunctions;
SortedSet<FuId>() NIntFunctions;
SortedSet<FuId>() LongFunctions;
bool IntTryParse;
bool LongTryParse;
bool DoubleTryParse;
bool StringAssign;
bool StringSubstring;
Expand Down Expand Up @@ -2003,12 +2001,12 @@ public class GenC : GenCCpp
WriteEnumHasFlag(obj, args, parent);
break;
case FuId.IntTryParse:
this.IntTryParse = true;
this.IntFunctions.Add(FuId.IntTryParse);
Write("FuInt");
WriteTryParse(obj, args);
break;
case FuId.LongTryParse:
this.LongTryParse = true;
this.LongFunctions.Add(FuId.IntTryParse);
Write("FuLong");
WriteTryParse(obj, args);
break;
Expand Down Expand Up @@ -3511,6 +3509,23 @@ public class GenC : GenCCpp
CloseBlock();
}

void WriteTryParseLibrary!(string signature, string call)
{
WriteNewLine();
Write("static bool Fu");
WriteLine(signature);
OpenBlock();
WriteLine("if (*str == '\\0')");
WriteLine("\treturn false;");
WriteLine("char *end;");
WriteLine("errno = 0;");
Write("*result = strto");
Write(call);
WriteLine(");");
WriteLine("return *end == '\\0' && errno == 0;");
CloseBlock();
}

void WriteIntLibrary!(string klassName, string type, SortedSet<FuId> methods)
{
if (methods.Contains(FuId.MathMin))
Expand All @@ -3534,34 +3549,19 @@ public class GenC : GenCCpp
WriteLine("return x < minValue ? minValue : x > maxValue ? maxValue : x;");
CloseBlock();
}
}

void WriteTryParseLibrary!(string signature, string call)
{
WriteNewLine();
Write("static bool Fu");
WriteLine(signature);
OpenBlock();
WriteLine("if (*str == '\\0')");
WriteLine("\treturn false;");
WriteLine("char *end;");
WriteLine("errno = 0;");
Write("*result = strto");
Write(call);
WriteLine(");");
WriteLine("return *end == '\\0' && errno == 0;");
CloseBlock();
if (methods.Contains(FuId.IntTryParse)) {
if (klassName == "Long")
WriteTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
else
WriteTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
}
}

void WriteLibrary!()
{
WriteIntLibrary("Int", "int", this.IntFunctions);
WriteIntLibrary("NInt", "ptrdiff_t", this.NIntFunctions);
WriteIntLibrary("Long", "int64_t", this.LongFunctions);
if (this.IntTryParse)
WriteTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
if (this.LongTryParse)
WriteTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
if (this.DoubleTryParse)
WriteTryParseLibrary("Double_TryParse(double *result, const char *str)", "d(str, &end");
if (this.StringAssign) {
Expand Down Expand Up @@ -3914,8 +3914,6 @@ public class GenC : GenCCpp
this.IntFunctions.Clear();
this.NIntFunctions.Clear();
this.LongFunctions.Clear();
this.IntTryParse = false;
this.LongTryParse = false;
this.DoubleTryParse = false;
this.StringAssign = false;
this.StringSubstring = false;
Expand Down
50 changes: 25 additions & 25 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11437,12 +11437,12 @@ void GenC::writeCallExpr(const FuExpr * obj, const FuMethod * method, const std:
writeEnumHasFlag(obj, args, parent);
break;
case FuId::intTryParse:
this->intTryParse = true;
this->intFunctions.insert(FuId::intTryParse);
write("FuInt");
writeTryParse(obj, args);
break;
case FuId::longTryParse:
this->longTryParse = true;
this->longFunctions.insert(FuId::intTryParse);
write("FuLong");
writeTryParse(obj, args);
break;
Expand Down Expand Up @@ -12956,6 +12956,23 @@ void GenC::writeIntMaxMin(std::string_view klassName, std::string_view method, s
closeBlock();
}

void GenC::writeTryParseLibrary(std::string_view signature, std::string_view call)
{
writeNewLine();
write("static bool Fu");
writeLine(signature);
openBlock();
writeLine("if (*str == '\\0')");
writeLine("\treturn false;");
writeLine("char *end;");
writeLine("errno = 0;");
write("*result = strto");
write(call);
writeLine(");");
writeLine("return *end == '\\0' && errno == 0;");
closeBlock();
}

void GenC::writeIntLibrary(std::string_view klassName, std::string_view type, const std::set<FuId> * methods)
{
if (methods->contains(FuId::mathMin))
Expand All @@ -12979,34 +12996,19 @@ void GenC::writeIntLibrary(std::string_view klassName, std::string_view type, co
writeLine("return x < minValue ? minValue : x > maxValue ? maxValue : x;");
closeBlock();
}
}

void GenC::writeTryParseLibrary(std::string_view signature, std::string_view call)
{
writeNewLine();
write("static bool Fu");
writeLine(signature);
openBlock();
writeLine("if (*str == '\\0')");
writeLine("\treturn false;");
writeLine("char *end;");
writeLine("errno = 0;");
write("*result = strto");
write(call);
writeLine(");");
writeLine("return *end == '\\0' && errno == 0;");
closeBlock();
if (methods->contains(FuId::intTryParse)) {
if (klassName == "Long")
writeTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
else
writeTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
}
}

void GenC::writeLibrary()
{
writeIntLibrary("Int", "int", &this->intFunctions);
writeIntLibrary("NInt", "ptrdiff_t", &this->nIntFunctions);
writeIntLibrary("Long", "int64_t", &this->longFunctions);
if (this->intTryParse)
writeTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
if (this->longTryParse)
writeTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
if (this->doubleTryParse)
writeTryParseLibrary("Double_TryParse(double *result, const char *str)", "d(str, &end");
if (this->stringAssign) {
Expand Down Expand Up @@ -13356,8 +13358,6 @@ void GenC::writeProgram(const FuProgram * program)
this->intFunctions.clear();
this->nIntFunctions.clear();
this->longFunctions.clear();
this->intTryParse = false;
this->longTryParse = false;
this->doubleTryParse = false;
this->stringAssign = false;
this->stringSubstring = false;
Expand Down
54 changes: 25 additions & 29 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9715,10 +9715,6 @@ public class GenC : GenCCpp

readonly SortedSet<FuId> LongFunctions = new SortedSet<FuId>();

bool IntTryParse;

bool LongTryParse;

bool DoubleTryParse;

bool StringAssign;
Expand Down Expand Up @@ -11697,12 +11693,12 @@ protected override void WriteCallExpr(FuExpr obj, FuMethod method, List<FuExpr>
WriteEnumHasFlag(obj, args, parent);
break;
case FuId.IntTryParse:
this.IntTryParse = true;
this.IntFunctions.Add(FuId.IntTryParse);
Write("FuInt");
WriteTryParse(obj, args);
break;
case FuId.LongTryParse:
this.LongTryParse = true;
this.LongFunctions.Add(FuId.IntTryParse);
Write("FuLong");
WriteTryParse(obj, args);
break;
Expand Down Expand Up @@ -13188,6 +13184,23 @@ void WriteIntMaxMin(string klassName, string method, string type, int op)
CloseBlock();
}

void WriteTryParseLibrary(string signature, string call)
{
WriteNewLine();
Write("static bool Fu");
WriteLine(signature);
OpenBlock();
WriteLine("if (*str == '\\0')");
WriteLine("\treturn false;");
WriteLine("char *end;");
WriteLine("errno = 0;");
Write("*result = strto");
Write(call);
WriteLine(");");
WriteLine("return *end == '\\0' && errno == 0;");
CloseBlock();
}

void WriteIntLibrary(string klassName, string type, SortedSet<FuId> methods)
{
if (methods.Contains(FuId.MathMin))
Expand All @@ -13211,34 +13224,19 @@ void WriteIntLibrary(string klassName, string type, SortedSet<FuId> methods)
WriteLine("return x < minValue ? minValue : x > maxValue ? maxValue : x;");
CloseBlock();
}
}

void WriteTryParseLibrary(string signature, string call)
{
WriteNewLine();
Write("static bool Fu");
WriteLine(signature);
OpenBlock();
WriteLine("if (*str == '\\0')");
WriteLine("\treturn false;");
WriteLine("char *end;");
WriteLine("errno = 0;");
Write("*result = strto");
Write(call);
WriteLine(");");
WriteLine("return *end == '\\0' && errno == 0;");
CloseBlock();
if (methods.Contains(FuId.IntTryParse)) {
if (klassName == "Long")
WriteTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
else
WriteTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
}
}

void WriteLibrary()
{
WriteIntLibrary("Int", "int", this.IntFunctions);
WriteIntLibrary("NInt", "ptrdiff_t", this.NIntFunctions);
WriteIntLibrary("Long", "int64_t", this.LongFunctions);
if (this.IntTryParse)
WriteTryParseLibrary("Int_TryParse(int *result, const char *str, int base)", "l(str, &end, base");
if (this.LongTryParse)
WriteTryParseLibrary("Long_TryParse(int64_t *result, const char *str, int base)", "ll(str, &end, base");
if (this.DoubleTryParse)
WriteTryParseLibrary("Double_TryParse(double *result, const char *str)", "d(str, &end");
if (this.StringAssign) {
Expand Down Expand Up @@ -13588,8 +13586,6 @@ public override void WriteProgram(FuProgram program)
this.IntFunctions.Clear();
this.NIntFunctions.Clear();
this.LongFunctions.Clear();
this.IntTryParse = false;
this.LongTryParse = false;
this.DoubleTryParse = false;
this.StringAssign = false;
this.StringSubstring = false;
Expand Down
4 changes: 1 addition & 3 deletions libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2227,8 +2227,6 @@ class GenC : public GenCCpp
std::set<FuId> intFunctions;
std::set<FuId> nIntFunctions;
std::set<FuId> longFunctions;
bool intTryParse;
bool longTryParse;
bool doubleTryParse;
bool stringAssign;
bool stringSubstring;
Expand Down Expand Up @@ -2339,8 +2337,8 @@ class GenC : public GenCCpp
void writeNewDelete(const FuClass * klass, bool define);
static bool canThrow(const FuType * type);
void writeIntMaxMin(std::string_view klassName, std::string_view method, std::string_view type, int op);
void writeIntLibrary(std::string_view klassName, std::string_view type, const std::set<FuId> * methods);
void writeTryParseLibrary(std::string_view signature, std::string_view call);
void writeIntLibrary(std::string_view klassName, std::string_view type, const std::set<FuId> * methods);
void writeLibrary();
};

Expand Down
Loading

0 comments on commit 3a7a1ab

Please sign in to comment.