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
This commit is contained in:
Kevin Caccamo 2018-08-22 03:52:11 -04:00 committed by Christoph Oelckers
parent a99b71e19a
commit 884de51b70
4 changed files with 56 additions and 5 deletions

View file

@ -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);

View file

@ -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] [{+ | <EFBFBD>}] [0 [{ x | X }]] [digits] [whitespace]
/* This state machine is based on a simplification of re2c's output for this input:
digits = [0-9];

View file

@ -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<FString>
// Compares two keys, returning zero if they are the same.
int Compare(const FString &left, const FString &right) { return left.Compare(right); }
};

View file

@ -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;