From 884de51b705be9c3e1ba71c8a826037d5c95ac18 Mon Sep 17 00:00:00 2001 From: Kevin Caccamo Date: Wed, 22 Aug 2018 03:52:11 -0400 Subject: [PATCH] Add the "RIndexOf" method to FString, which works like String.lastIndexOf from JavaScript RIndexOf returns the index where the substring starts, instead of the index where the substring ends - 1. Deprecate the LastIndexOf method of StringStruct --- src/scripting/thingdef_data.cpp | 12 ++++++++-- src/zstring.cpp | 39 ++++++++++++++++++++++++++++++++- src/zstring.h | 7 +++++- wadsrc/static/zscript/base.txt | 3 ++- 4 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/scripting/thingdef_data.cpp b/src/scripting/thingdef_data.cpp index ca6bee2a4..8f4a209d0 100644 --- a/src/scripting/thingdef_data.cpp +++ b/src/scripting/thingdef_data.cpp @@ -327,7 +327,7 @@ static FFlagDef ActorFlagDefs[]= DEFINE_FLAG(RF, INVISIBLE, AActor, renderflags), DEFINE_FLAG(RF, FORCEYBILLBOARD, AActor, renderflags), DEFINE_FLAG(RF, FORCEXYBILLBOARD, AActor, renderflags), - DEFINE_FLAG(RF, ROLLSPRITE, AActor, renderflags), // [marrub] roll the sprite billboard + DEFINE_FLAG(RF, ROLLSPRITE, AActor, renderflags), // [marrub] roll the sprite billboard // [fgsfds] Flat sprites DEFINE_FLAG(RF, FLATSPRITE, AActor, renderflags), DEFINE_FLAG(RF, WALLSPRITE, AActor, renderflags), @@ -593,7 +593,7 @@ FFlagDef *FindFlag (const PClass *type, const char *part1, const char *part2, bo //========================================================================== // -// Gets the name of an actor flag +// Gets the name of an actor flag // //========================================================================== @@ -1273,6 +1273,14 @@ DEFINE_ACTION_FUNCTION(FStringStruct, LastIndexOf) ACTION_RETURN_INT(self->LastIndexOf(substr, endIndex)); } +DEFINE_ACTION_FUNCTION(FStringStruct, RIndexOf) +{ + PARAM_SELF_STRUCT_PROLOGUE(FString); + PARAM_STRING(substr); + PARAM_INT_DEF(endIndex); + ACTION_RETURN_INT(self->RIndexOf(substr, endIndex)); +} + DEFINE_ACTION_FUNCTION(FStringStruct, ToUpper) { PARAM_SELF_STRUCT_PROLOGUE(FString); diff --git a/src/zstring.cpp b/src/zstring.cpp index ea9c249bb..e207cdb6b 100644 --- a/src/zstring.cpp +++ b/src/zstring.cpp @@ -596,6 +596,43 @@ long FString::LastIndexOfAny (const char *charset, long endIndex) const return -1; } +long FString::RIndexOf (const FString &substr) const +{ + return RIndexOf(substr.Chars, Len() - substr.Len(), substr.Len()); +} + +long FString::RIndexOf (const FString &substr, long endIndex) const +{ + return RIndexOf(substr.Chars, endIndex, substr.Len()); +} + +long FString::RIndexOf (const char *substr) const +{ + return RIndexOf(substr, Len() - strlen(substr), strlen(substr)); +} + +long FString::RIndexOf (const char *substr, long endIndex) const +{ + return RIndexOf(substr, endIndex, strlen(substr)); +} + +long FString::RIndexOf (const char *substr, long endIndex, size_t substrlen) const +{ + if ((size_t)endIndex + substrlen > Len()) + { + endIndex = Len() - substrlen; + } + while (endIndex >= 0) + { + if (strncmp (substr, Chars + endIndex, substrlen) == 0) + { + return endIndex; + } + endIndex--; + } + return -1; +} + void FString::ToUpper () { LockBuffer(); @@ -1003,7 +1040,7 @@ void FString::Substitute (const char *oldstr, const char *newstr, size_t oldstrl bool FString::IsInt () const { - // String must match: [whitespace] [{+ | –}] [0 [{ x | X }]] [digits] [whitespace] + // String must match: [whitespace] [{+ | �}] [0 [{ x | X }]] [digits] [whitespace] /* This state machine is based on a simplification of re2c's output for this input: digits = [0-9]; diff --git a/src/zstring.h b/src/zstring.h index 5ab267414..d61408dc7 100644 --- a/src/zstring.h +++ b/src/zstring.h @@ -214,6 +214,12 @@ public: long LastIndexOfAny (const FString &charset, long endIndex) const; long LastIndexOfAny (const char *charset, long endIndex) const; + long RIndexOf (const FString &substr) const; + long RIndexOf (const FString &substr, long endIndex) const; + long RIndexOf (const char *substr) const; + long RIndexOf (const char *substr, long endIndex) const; + long RIndexOf (const char *substr, long endIndex, size_t substrlen) const; + void ToUpper (); void ToLower (); void SwapCase (); @@ -463,4 +469,3 @@ template<> struct THashTraits // Compares two keys, returning zero if they are the same. int Compare(const FString &left, const FString &right) { return left.Compare(right); } }; - diff --git a/wadsrc/static/zscript/base.txt b/wadsrc/static/zscript/base.txt index 498f6ae71..90c847a23 100644 --- a/wadsrc/static/zscript/base.txt +++ b/wadsrc/static/zscript/base.txt @@ -815,7 +815,8 @@ struct StringStruct native native int CharCodeAt(int pos) const; native String Filter(); native int IndexOf(String substr, int startIndex = 0) const; - native int LastIndexOf(String substr, int endIndex = 2147483647) const; + deprecated("3.5") native int LastIndexOf(String substr, int endIndex = 2147483647) const; + native int RIndexOf(String substr, int endIndex = 2147483647) const; native void ToUpper(); native void ToLower(); native int ToInt(int base = 0) const;