José fallen into the recycle bin?

Started by Charles Pegge, April 04, 2025, 04:46:28 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Charles Pegge


José Roca

#1
It is a work in progress, subject to numerous changes. It is better to look at my repository in GitHub. Otherwise, Google and company will return code that is outdated.

I'm finishing the include file that contain the string functions, now prefixed as "wstr" ("wcs" is already used by Microsoft). The ones that aren't complicated or that need speed, are hard coded; the ones that are complex or with many variations (case-sensitive, case-insensitive, single delimiters or ANY delimiters, local or global), are being coded using my wrapper class CRegExp and regular expressions patterns. This way, I can have only one function instead of 4 or 6.

Additionally, besides the ready to use string functions, you can use the CRegExp class to write your own code or procedures if what you need is not covered by these ready to use functions. You only need to find the appropriate pattern. This is where the AI can help. I'm unable to remember how to construct these darn patterns.

Thanks for your interest.

José Roca

#2
For example, I have just finished a function to extract substrings.

' ========================================================================================
PRIVATE FUNCTION wstrExtract (BYREF wszSourceString AS CONST WSTRING, BYREF wszMatchString AS CONST WSTRING, BYVAL IgnoreCase AS BOOLEAN = TRUE) AS DWSTRING
   IF wszSourceString = "" OR wszMatchString ="" THEN RETURN wszSourceString
   DIM pRegExp AS CRegExp
   pRegExp.Global = FALSE
   pRegExp.IgnoreCase = IgnoreCase
   ' // This pattern matches the delimiter passed (can consist of a group of one or more characters)
   ' // Passing wszMatchString enclosed between "[]" will match any of the individual characters
   pRegExp.Pattern = "^(.*?)" & wszMatchString
   SetLastError(pRegExp.Execute(wszSourceString))
   iF pRegExp.SubMatchesCount THEN
      RETURN STRPTR(pRegExp.SubMatchValue(0, 0))
   END IF
   ' // If wszMatchString is not found return all of wszSourceString
   RETURN wszSourceString
END FUNCTION
' ========================================================================================

DIM dws AS DWSTRING = "abacadabra"
Print wstrExtract(dws, "cad")
' Output: aba - match on "cad"

DIM dws AS DWSTRING = "abacadabra"
Print wstrExtract(dws, "[dr]")
' Output: abaca - match on "d"

The difference is that to search for individual characters to match, you enclose them between "[]": "[dr]" will search for d or r; "cad" will search for "cad".

The same function can be used for different purposes and can be case-sensitive or insensitive.

Now I will write an overloaded function with two delimiters to extract text between them.