Jump to content

Created some useful string functions (SUP_F4SE required)


Recommended Posts

Hello peeps,
I made some string functions based on functions found in SUP. Basically this will help you keep the number of F4SE extensions down by providing some stuff that is found in other libraries (StringUtil, LL_4Play etc), complementing SUP.
Having said that, BakaUtil (where StringUtil for FO4 resides) and LL_4Play are themselves great extensions with lots of other functions so have a look at those too, imo. Their versions will more than likely be faster than mine because they operate directly on memory addresses with null pointers.
Create a character array from a string

String[] Function CharArray(String IN) Global
String[] OUT = new String[0]
Int i = 0
While (i < StringGetLength(IN))
OUT.Add(StringFindSubString(IN, i, i))
i += 1
EndWhile
Return OUT
EndFunction

Turn a string array into one string

String Function ConcatToString(String[] IN) Global
String OUT = ""
Int i = 0
While (i < IN.Length)
OUT += IN
i += 1
EndWhile
Return OUT
EndFunction

Turn a string into an array, split at custom delimiter

Removed. Just use the SUP_F4SE 8.65 version and upward

Turn a hex int (string representation) into a decimal int

int Function HxDc(String IN) Global

String[] HXTBL = CharArray("0123456789ABCDEF")
String[] INARR = CharArray(IN)
Float SUM = 0.0
Int i = INARR.Length
Int r = HXTBL.Length
Int a = 0

While (i > 0 && a < INARR.Length)
If (INARR[i-1] == HXTBL[r-1])
SUM = SUM + ((r-1) as float) * Math.pow(16.0, (a as float))
i -= 1
a += 1
r = HXTBL.Length
Else
r -=1
Endif
EndWhile
Return SUM as int
EndFunction

Turn a decimal Int into a Hex string (questionable use, perhaps for console commands)

String Function DcHx(Int IN) Global
String[] HXTBL = CharArray("0123456789ABCDEF")
Int Base = 16
String[] OUT = new String[0]
If (IN < 17)
OUT.Add(HXTBL[iN], 1)
Else
While (IN != 0)
OUT.Insert(HXTBL[iN % Base], 0)
IN /= Base
EndWhile
Endif
Return ConcatToString(OUT)
EndFunction

Note that these functions "need each other". Feedback and improvements appreciated. Will return with more.
Link to comment
Share on other sites

Had some trouble getting ArrayDelim() function to work; Presumably due to me using weird delimiter chars that would need escaping for the regex or whatever.

But I don't understand regex. So I made a new function that works without 'em:

String[] Function StringSplit(String inStr, String delimiter = ",")
    If (inStr == "")
        Return new String[0]
    EndIf
    String[] outStr = new String[1]
    Int i = 0
    Int o = 0
    String curStr = StringFindSubString(inStr, i, i)
    While (curStr != "")
        If (curStr == delimiter)
            outStr.Add("")
            o += 1
        Else
            outStr[o] += curStr
        EndIf 
        i += 1
        curStr = StringFindSubString(inStr, i, i)
    EndWhile
    Return outStr
EndFunction
Link to comment
Share on other sites

Update: SUP F4SE 8.65 has new function to split string into array:

String[] Function SplitStringEx(String StringIN, String delimeter, int iPosStart) global native

It is SO MUCH faster than my function above.

Awesome! Yeah I also ended up having trouble with the one I made. Like you said, if it messes with the regex lookahead pattern it'll behave unpredictably.

 

Removing mine, let's just use Tommi's.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...