Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more string stocks #2186

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sirdigbot
Copy link
Contributor

@sirdigbot sirdigbot commented Jun 29, 2024

Sorry in advance for the large PR

This is the new PR to add more string stocks since the old one went stale and then got closed.
Below is a list of every single useful string function I could find across other common languages (mainly JS and python), as well as a handful of functions I have used in my own projects that I think are useful. I've also added explanations where necessary but they're pretty self-explanatory.

This list is more of a starting point--I asked in the discord a while ago but never got a clear answer as to which are useful and which aren't. So I'm throwing them all at you and you can dissect which ones should stay and what should be added.

Important Note: Apart from a few, these functions are largely untested and probably not optimized well yet.

They are almost certainly buggy and need proper tests written. But I'd rather not write tests (or docstrings) for functions we're not even gonna keep so one step at a time.

I have also commented with <<<<<<<<<<<<<<<<<<<<<<<<<<< in places where I have questions that need answering.

Function List

Rewritten Funcs from old PR
IsStringInteger -- Is a string an integer (i.e. supported by StringToIntEx)
IsStringDecimal -- Is a string a decimal (i.e. supported by StringToFloatEx)
StringToLower
StringToUpper

Identifying
IsStringUpper -- Is string all uppercase (optionally require no whitespace)
IsStringLower -- Is string all lowercase (optionally require no whitespace)
IsStringTitle -- Is String In Titlecase
IsStringASCII -- Does string only use valid ASCII characters
IsStringUTF8 -- Does string use any non-ASCII characters (valid or not)
IsStringAlpha -- Is string all alphabet characters (optionally require no whitespace)
IsStringAlphaNumeric
IsCharAlphaNumeric
IsCharHex
IsStringHex -- Is string all hex characters (optionally require no whitespace)
IsCharVisible -- Is a character visible when printed--no control chars, optionally allow whitespace
IsStringVisible
IsStringWhitespace -- Is string all whitespace
StringEndsWith
StringStartsWith

Cleaning
TrimStringEx -- Trim start/end of string but specify which characters to remove (as a string/charset)
TrimStringLeft -- Trim whitespace from left of string (see TODO)
TrimStringRight
TrimStringSuffix -- Trim an exact suffix from the string (if it exists)
TrimStringPrefix
FilterString -- Removes all characters matching charset from string
NormalizeString -- Trim whitespace, strip quotes and force to lowercase. Useful for sanitizing string comparisons.
TruncateString -- Truncate and add trailing ... to indicate text was clipped.

Mutation
StringToTitle -- To Title Case
StringLeftPad -- Add padding characters to the left of a string
StringRightPad
StringCenterPad
StringRepeat -- Fill a buffer with a repetition of a string (optionally truncate)

Misc
CharAt -- Ok this one needs a demo:

stock char CharAt(char c)
{
    return c;
}

// Shorthand for doing the following, in cases where reading a char will read the rest of the string
char c = str[offset]
DoThingWithCharOrErroneouslyAsString(c);

DoThingWithCharOrErroneouslyAsString(CharAt(str[offset]));

strncopy -- strcopy with a length parameter.
StringToIntStrict -- StringToIntEx, but fails if string is not EXACTLY a valid and convertible number
StringToFloatStrict
ExplodeStringToList -- Separate a delimited string to an ArrayList
SubstringToInt -- StringToInt but with a length param, allowing it to specify a substring.
SubstringToIntEx
SubstringToFloat
SubstringToFloatEx
SubstringCount -- Count substrings in string.

TODO

  • StringStartsWith could just be a wrapper for strncmp, would that be better?
  • TrimStringLeft and TrimStringRight should allow you to specify a charset instead of just only affecting whitespace but this would require having some sort of whitespace constant or defaulting to a string of whitespace
  • NormalizeString needs to immediately return for empty string (strlen - 1 will OOB)

Some concerns I have

  • Return values might be wrong in some places
  • Unsure about performance since many functions call things like strlen and strcopy instead of doing things the harder way (since i'm dumb)
  • Some functions might have bad API, or could be generalised better.
  • Bad naming of functions (IsStringUTF8 for example is bad)
  • TBA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant