AfxStrParseCountAny bug

Started by Paul Squires, July 28, 2026, 05:46:33 PM

Previous topic - Next topic

Paul Squires

Hi José,

Looks like AfxStrParseCountAny() has a little bug in it.

' ========================================================================================
' * Return the count of delimited fields from a string expression.
' If wszMainStr is empty (a null string) or contains no delimiter character(s), the string
' is considered to contain exactly one sub-field. In this case, AfxStrParseCountAny returns the value 1.
' Delimiter contains a set of characters (one or more), any of which may act as a delimiter character.
' Delimiters are case-sensitive.
' Example: DIM nCount AS LONG = AfxStrParseCountAny("1;2,3", ",;")
' ========================================================================================
PRIVATE FUNCTION AfxStrParseCountAny (BYREF wszMainStr AS CONST WSTRING, BYREF wszDelimiter AS CONST WSTRING = ",") AS LONG
   DIM nCount AS LONG = 1
   DIM nPos AS LONG = 1
   DO
      nPos = INSTR(nPos, wszMainStr, ANY wszDelimiter)
      IF nPos = 0 THEN EXIT DO
      nCount += 1
      nPos += LEN(wszDelimiter)   '<---- THIS IS THE PROBLEM
      ' nPos += 1   '<---- THIS IS THE FIX
   LOOP
   RETURN nCount
END FUNCTION
' ========================================================================================
Paul Squires
PlanetSquires Software

José Roca

#1
You're right. I have modified it.