Skip to content

Commit

Permalink
Add the "RIndexOf" method to FString, which works like String.lastInd…
Browse files Browse the repository at this point in the history
…exOf 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
  • Loading branch information
Talon1024 authored and coelckers committed Aug 25, 2018
1 parent 3d44b9f commit 5ae6137
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/scripting/thingdef_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,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),
Expand Down Expand Up @@ -592,7 +592,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
//
//==========================================================================

Expand Down Expand Up @@ -1272,6 +1272,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);
Expand Down
39 changes: 38 additions & 1 deletion src/zstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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];
Expand Down
7 changes: 6 additions & 1 deletion src/zstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,12 @@ class FString
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 ();
Expand Down Expand Up @@ -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); }
};

3 changes: 2 additions & 1 deletion wadsrc/static/zscript/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,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;
Expand Down

0 comments on commit 5ae6137

Please sign in to comment.