home *** CD-ROM | disk | FTP | other *** search
- // STRXCMP.SCP - Case in/sensitive string comparison functions
- // Copyright (c) 2000, Vector Networks Limited
- // All Rights Reserved
- //
- // Revision History:
- // 5.3 31-Jul-00 DB - Created.
- // 01-Aug-00 AB - Change "-1" result to 2 when String2 is greater
- // than String1.
- // 02-Aug-00 DB - Add number of characters to compare in to
- // StrXCmp, and add StrNCmp and StrNICmp functions.
- // 04-Aug-00 DB - Comment out Print statements
-
- $INCLUDE "ASC.SCP"
-
- // StrCmp compares two strings. It uses StrXCmp in case-sensitive
- // mode and returns the result.
-
- Function StrCmp (String1 as String, String2 as String) as Integer
- StrCmp = StrXCmp (String1, String2, 0, TRUE)
- End Function
-
- // StrICmp compares two strings. It uses StrXCmp in case-insensitive
- // mode and returns the result.
-
- Function StrICmp (String1 as String, String2 as String) as Integer
- StrICmp = StrXCmp (String1, String2, 0, FALSE)
- End Function
-
- // StrNCmp compares N characters of two strings. It uses StrXCmp
- // in case-sensitive mode and returns the result.
-
- Function StrNCmp (String1 as String, String2 as String, MaxChars as Integer) as Integer
- StrNCmp = StrXCmp (String1, String2, MaxChars, TRUE)
- End Function
-
- // StrNICmp compares N characters of two strings. It uses StrXCmp
- // in case-insensitive mode and returns the result.
-
- Function StrNICmp (String1 as String, String2 as String, MaxChars as Integer) as Integer
- StrNICmp = StrXCmp (String1, String2, MaxChars, FALSE)
- End Function
-
- // StrXCmp compares two strings for equality. It can be used in both
- // case-sensitive and - insensitive modes, as determined by the third
- // parameter. TRUE = case-sensitive. FALSE = case-insensitive.
- //
- // In the manner of the standard 'C' library routine strcmp (), it returns
- // -1 if String1 is less than String2, 0 if they are the same, or +1 if
- // String1 is greater than String2. Strings are compared character by
- // character, using Asc () to obtain integer values.
- //
- // One string is less than another if it contains a character of lower
- // integer value, e.g. "a" is less than "b". They are equal if they contain
- // the same number of characters, which are also the same integer values,
- // when the case-sensitive or -insensitive comparison is performed.
-
- Function StrXCmp (String1 as String, String2 as String, MaxChars as Integer, CaseSens as Integer) as Integer
- Dim Len1, Len2, Char1, Char2, Asc1, Asc2, Chars, Result
-
- Result = 0
-
- // First, check the input parameters
-
- If IsString (String1) then
- Len1 = Len (String1)
- Else
- Len1 = 0
- Endif
-
- If IsString (String2) then
- Len2 = Len (String2)
- Else
- Len2 = 0
- Endif
-
- // See which is shorter - either could be empty
-
- If Len1 < Len2 Then
- Chars = Len1
- Else
- Chars = Len2
- Endif
-
- // But, we might have been asked to compare only so many
- // characters of each string. Check next.
-
- If MaxChars > 0 Then
- If MaxChars < Chars Then
- Chars = MaxChars
- Endif
- Endif
-
- // If we have two non-empty strings, there's work to do
-
- If Chars > 0 Then
-
- // Print "Comparing ", String1, " with ", String2, " to ", Chars, " characters"
-
- // Now, we loop through both strings, checking each character
-
- For n = 1 To Chars
- Char1 = Mid (String1, n, 1)
- Char2 = Mid (String2, n, 1)
-
- // In case-insensitive mode, convert both to UPPER CASE
-
- If CaseSens = FALSE Then
- Char1 = UCase (Char1)
- Char2 = UCase (Char2)
- Endif
-
- // Now, convert both to integers
-
- Asc1 = Asc (Char1)
- Asc2 = Asc (Char2)
-
- // Print "Character ", n, " 1 = ", Char1, " (", Asc1, ") 2 = ", Char2, " (", Asc2, ")"
-
- // and compare them
-
- If Asc1 > Asc2 Then
- // Print "String1 is greater after ", n, " characters"
- Result = 1
- Endif
- If Asc1 < Asc2 Then
- // Print "String1 is lesser after ", n, " characters"
- Result = 2
- Endif
- If Result != 0 Then
- // Print "Exiting from loop after ", n, " characters"
- Exit For
- Endif
- Next
-
- // Here, if Result is still 0, the common part of the two
- // strings is the same. If we were comparing the full
- // string length(s), then the longer string is greater. If we
- // were only comparing MaxChars, then we may already
- // have a correct answer. Chars is non-zero, so if Chars is
- // not equal to MaxChars, and it has to be less if so, then
- // we not have not finished comparing yet...
-
- If MaxChars != Chars Then
- If Result = 0 Then
- If Len1 > Chars Then
- // Print "String1 is longer"
- Result = 1
- Endif
- If Len2 > Chars Then
- // Print "String1 is shorter"
- Result = 2
- Endif
- Endif
- Endif
- Else
-
- // Here, one or both strings is empty. See which (or both)
-
- If Len1 = Len2 Then Result = 0
- If Len1 > Len2 Then Result = 1
- If Len1 < Len2 Then Result = 2
- Endif
-
- // Print "Returning result ", Result
-
- StrXCmp = Result
- End Function
-