[Added] Make FString::LastIndexOf work like Javascript's equivalent

Moderator: GZDoom Developers

Make FString::LastIndexOf work like Javascript's equivalent

Postby Talon1024 » Wed Aug 22, 2018 4:58 am

Why? FString::LastIndexOf is weird. For example, if you run this ZScript code:
Code: Select allExpand view
class StringTestEventHandler : EventHandler {
   override void PlayerEntered(PlayerEvent e) {
      static const String testStrings[] = { "hello", "hello.obj", "hello.obj.world", "hello.obj.obj.obj", "hello.obj.obj.obj.world" };

      for (int i = 0; i < 5; i++) {
         String test = testStrings[i];
         Console.Printf("Test string: \"%s\"\n", test);
         Console.Printf("LastIndexOf(\".obj\"): %d\n", test.LastIndexOf(".obj"));
         Console.Printf("LastIndexOf(\".\"): %d\n", test.LastIndexOf("."));
         Console.Printf("LastIndexOf(\"eeee\"): %d\n", test.LastIndexOf("eeee"));
         Console.Printf("LastIndexOf(\"eeeeeeeeeeeeeeeeeeeeeeeeeeeeee\"): %d\n", test.LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"));
      }
   }
}

The results from the current master branch are:
Code: Select allExpand view
Test string: "hello"
LastIndexOf(".obj"): -1
LastIndexOf("."): -1
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj"
LastIndexOf(".obj"): 8
LastIndexOf("."): 5
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.world"
LastIndexOf(".obj"): 8
LastIndexOf("."): 9
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.obj.obj"
LastIndexOf(".obj"): 16
LastIndexOf("."): 13
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.obj.obj.world"
LastIndexOf(".obj"): 16
LastIndexOf("."): 17
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1


I'm used to the way String.lastIndexOf works in JavaScript, and I'm sure most other developers are as well. The results from this code submission are:
Code: Select allExpand view
Test string: "hello"
LastIndexOf(".obj"): -1
LastIndexOf("."): -1
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj"
LastIndexOf(".obj"): 5
LastIndexOf("."): 5
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.world"
LastIndexOf(".obj"): 5
LastIndexOf("."): 9
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.obj.obj"
LastIndexOf(".obj"): 13
LastIndexOf("."): 13
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1

Test string: "hello.obj.obj.obj.world"
LastIndexOf(".obj"): 13
LastIndexOf("."): 17
LastIndexOf("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1


I just want to make sure I'm not breaking anything by making these changes.

Pull request: https://github.com/coelckers/gzdoom/pull/555
Talon1024
 
 
 
Joined: 27 Jun 2016
Github ID: Talon1024
Operating System: Debian-like Linux (Debian, Ubuntu, Kali, Mint, etc) 64-bit
Graphics Processor: nVidia GeForce/GTX/Quadro/etc (Modern GZDoom)

Re: Make FString::LastIndexOf work like Javascript's equival

Postby phantombeta » Wed Aug 22, 2018 5:10 am

This will very likely break any mods that expect the current behaviour.
User avatar
phantombeta
In the meadow of sinful thoughts, every flower's a perfect one
 
Joined: 02 May 2013
Location: The United Soviet Socialist Dictatorship of Hueland
Discord: phantombeta#2461
Twitch ID: phantombeta_
Github ID: Doom2fan
Operating System: Windows 10/8.1/8 64-bit

Re: Make FString::LastIndexOf work like Javascript's equival

Postby Graf Zahl » Wed Aug 22, 2018 5:54 am

Correct. While this function is clearly broken thanks to the underlying implementation, just altering it is not acceptable. The fixed version needs to be a new function with a different name and the old one needs to be deprecated for coming ZScript versions.
User avatar
Graf Zahl
Lead GZDoom Developer
 
Joined: 19 Jul 2003
Location: Germany

Re: Make FString::LastIndexOf work like Javascript's equival

Postby Talon1024 » Wed Aug 22, 2018 1:16 pm

OK, I added a new "IndexOfLast" function to FString in order to maintain backwards compatibility. Here are the results:

Code: Select allExpand view
Test string: "hello"
LastIndexOf(".obj"): -1
IndexOfLast(".obj"): -1
LastIndexOf("."): -1
IndexOfLast("."): -1
LastIndexOf("eeee"): -1
IndexOfLast("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
IndexOfLast("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
Test string: "hello.obj"
LastIndexOf(".obj"): 8
IndexOfLast(".obj"): 5
LastIndexOf("."): 5
IndexOfLast("."): 5
LastIndexOf("eeee"): -1
IndexOfLast("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
IndexOfLast("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
Test string: "hello.obj.world"
LastIndexOf(".obj"): 8
IndexOfLast(".obj"): 5
LastIndexOf("."): 9
IndexOfLast("."): 9
LastIndexOf("eeee"): -1
IndexOfLast("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
IndexOfLast("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
Test string: "hello.obj.obj.obj"
LastIndexOf(".obj"): 16
IndexOfLast(".obj"): 13
LastIndexOf("."): 13
IndexOfLast("."): 13
LastIndexOf("eeee"): -1
IndexOfLast("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
IndexOfLast("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
Test string: "hello.obj.obj.obj.world"
LastIndexOf(".obj"): 16
IndexOfLast(".obj"): 13
LastIndexOf("."): 17
IndexOfLast("."): 17
LastIndexOf("eeee"): -1
IndexOfLast("eeee"): -1
LastIndexOf("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
IndexOfLast("eeeeeeeeeeeeeeeeeeeeeeeeeeeeee"): -1
Talon1024
 
 
 
Joined: 27 Jun 2016
Github ID: Talon1024
Operating System: Debian-like Linux (Debian, Ubuntu, Kali, Mint, etc) 64-bit
Graphics Processor: nVidia GeForce/GTX/Quadro/etc (Modern GZDoom)

Re: Make FString::LastIndexOf work like Javascript's equival

Postby Blue Shadow » Sun Aug 26, 2018 8:05 am

This should be closed, since it's been added.
User avatar
Blue Shadow
 
Joined: 14 Nov 2010
Operating System: Windows 10/8.1/8 64-bit
Graphics Processor: ATI Radeon (Modern GZDoom)


Return to Closed Feature Suggestions

Who is online

Users browsing this forum: No registered users and 3 guests