gbdev / gbdev/rgbds

[Feature request] String starts-with and ends-with

Open
#1,893 2 comments 0 reactions 0 assignees View on GitHub
enhancement rgbasm
Dominant language
C++
Stars
1.6k
Forks
193
Avg merge
1d 33m
Merged PRs (30d)
23

Description

Checking whether a string has a certain prefix or suffix is a common operation, which is why even high-level programming languages have added methods for it when they already had general-purpose substring-finding. (For example, C++ added `.starts_with` and `.ends_with` to `std::string` in C++20, and Javascript added `.startsWith` and `.endsWith` in ECMAScript 2015.)

I commented in #1884 that `^==` and `$==` operators would make sense, in a parallel to regex `^` and `$`, or CSS `^=` and `$=`. But even if it's not a common enough need to warrant operators, functions like `STRSTARTS` and `STRENDS` would also be useful.

Compare:

| Current | Functions | Operators |
| --- | --- | --- |
| `STRFIND("\1", "prefix") == 0` | `STRSTARTS("\1", "prefix")` | `"\1" ^== "prefix"` |
| `STRRFIND("\1", "suffix") == STRLEN("\1") - STRLEN("suffix")` | `STRENDS("\1", "suffix")` | `"\1" $== "suffix"` |

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.