home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Demo / PCDUO / data1.cab / Script_Samples / STRXCMP.SCP < prev    next >
Encoding:
Text File  |  2003-11-28  |  5.5 KB  |  168 lines

  1. // STRXCMP.SCP - Case in/sensitive string comparison functions
  2. // Copyright (c) 2000, Vector Networks Limited
  3. // All Rights Reserved
  4. //
  5. // Revision History:
  6. //  5.3 31-Jul-00 DB  - Created.
  7. //      01-Aug-00 AB  - Change "-1" result to 2 when String2 is greater
  8. //                      than String1.
  9. //      02-Aug-00 DB  - Add number of characters to compare in to
  10. //                      StrXCmp, and add StrNCmp and StrNICmp functions.
  11. //      04-Aug-00 DB  - Comment out Print statements
  12.  
  13. $INCLUDE "ASC.SCP"
  14.  
  15. //  StrCmp compares two strings. It uses StrXCmp in case-sensitive 
  16. //  mode and returns the result.
  17.  
  18. Function StrCmp (String1 as String, String2 as String) as Integer
  19.     StrCmp = StrXCmp (String1, String2, 0, TRUE)
  20. End Function
  21.  
  22. //  StrICmp compares two strings. It uses StrXCmp in case-insensitive 
  23. //  mode and returns the result.
  24.  
  25. Function StrICmp (String1 as String, String2 as String) as Integer
  26.     StrICmp = StrXCmp (String1, String2, 0, FALSE)
  27. End Function
  28.  
  29. //  StrNCmp compares N characters of two strings. It uses StrXCmp 
  30. //  in case-sensitive mode and returns the result.
  31.  
  32. Function StrNCmp (String1 as String, String2 as String, MaxChars as Integer) as Integer
  33.     StrNCmp = StrXCmp (String1, String2, MaxChars, TRUE)
  34. End Function
  35.  
  36. //  StrNICmp compares N characters of two strings. It uses StrXCmp 
  37. //  in case-insensitive mode and returns the result.
  38.  
  39. Function StrNICmp (String1 as String, String2 as String, MaxChars as Integer) as Integer
  40.     StrNICmp = StrXCmp (String1, String2, MaxChars, FALSE)
  41. End Function
  42.  
  43. //  StrXCmp compares two strings for equality. It can be used in both
  44. //  case-sensitive and - insensitive modes, as determined by the third
  45. //  parameter. TRUE = case-sensitive. FALSE = case-insensitive.
  46. //
  47. //  In the manner of the standard 'C' library routine strcmp (), it returns 
  48. //  -1 if String1 is less than String2, 0 if they are the same, or +1 if 
  49. //  String1 is greater than String2. Strings are compared character by 
  50. //  character, using Asc () to obtain integer values. 
  51. //
  52. //  One string is less than another if it contains a character of lower 
  53. //  integer value, e.g. "a" is less than "b". They are equal if they contain 
  54. //  the same number of characters, which are also the same integer values, 
  55. //  when the case-sensitive or -insensitive comparison is performed.
  56.  
  57. Function StrXCmp (String1 as String, String2 as String, MaxChars as Integer, CaseSens as Integer) as Integer
  58. Dim Len1, Len2, Char1, Char2, Asc1, Asc2, Chars, Result
  59.  
  60.     Result = 0
  61.  
  62.     //  First, check the input parameters
  63.  
  64.     If IsString (String1) then
  65.         Len1 = Len (String1)
  66.     Else
  67.         Len1 = 0
  68.     Endif
  69.  
  70.     If IsString (String2) then
  71.         Len2 = Len (String2)
  72.     Else
  73.         Len2 = 0
  74.     Endif
  75.  
  76.     //  See which is shorter - either could be empty
  77.  
  78.     If Len1 < Len2 Then
  79.         Chars = Len1
  80.     Else
  81.         Chars = Len2
  82.     Endif
  83.  
  84.     //  But, we might have been asked to compare only so many
  85.     //  characters of each string. Check next.
  86.  
  87.     If MaxChars > 0 Then
  88.         If MaxChars < Chars Then
  89.             Chars = MaxChars
  90.         Endif
  91.     Endif
  92.  
  93.     //  If we have two non-empty strings, there's work to do
  94.  
  95.     If Chars > 0 Then
  96.  
  97. //        Print "Comparing ", String1, " with ", String2, " to ", Chars, " characters"
  98.  
  99.         //  Now, we loop through both strings, checking each character
  100.  
  101.         For n = 1 To Chars 
  102.             Char1 = Mid (String1, n, 1)
  103.             Char2 = Mid (String2, n, 1)
  104.  
  105.             //  In case-insensitive mode, convert both to UPPER CASE
  106.  
  107.             If CaseSens = FALSE Then
  108.                 Char1 = UCase (Char1)
  109.                 Char2 = UCase (Char2)
  110.             Endif
  111.  
  112.             //  Now, convert both to integers
  113.  
  114.             Asc1 = Asc (Char1)
  115.             Asc2 = Asc (Char2)
  116.  
  117. //            Print "Character ", n, " 1 = ", Char1, " (", Asc1, ") 2 = ", Char2, " (", Asc2, ")"
  118.  
  119.             //  and compare them
  120.  
  121.             If Asc1 > Asc2 Then
  122. //                Print "String1 is greater after ", n, " characters"
  123.                 Result = 1
  124.             Endif
  125.             If Asc1 < Asc2 Then
  126. //                Print "String1 is lesser after ", n, " characters"
  127.                 Result = 2
  128.             Endif
  129.             If Result != 0 Then
  130. //                Print "Exiting from loop after ", n, " characters"
  131.                 Exit For
  132.             Endif
  133.         Next
  134.  
  135.         //  Here, if Result is still 0, the common part of the two 
  136.         //  strings is the same. If we were comparing the full
  137.         //  string length(s), then the longer string is greater. If we
  138.         //  were only comparing MaxChars, then we may already 
  139.         //  have a correct answer. Chars is non-zero, so if Chars is 
  140.         //  not equal to MaxChars, and it has to be less if so, then 
  141.         //  we not have not finished comparing yet...
  142.  
  143.         If MaxChars != Chars Then
  144.             If Result = 0 Then
  145.                 If Len1 > Chars Then 
  146. //                    Print "String1 is longer"
  147.                     Result = 1
  148.                 Endif
  149.                 If Len2 > Chars Then 
  150. //                    Print "String1 is shorter"
  151.                     Result = 2
  152.                 Endif
  153.             Endif
  154.         Endif
  155.     Else
  156.  
  157.         //  Here, one or both strings is empty. See which (or both)
  158.  
  159.         If Len1 = Len2 Then Result = 0
  160.         If Len1 > Len2 Then Result = 1
  161.         If Len1 < Len2 Then Result = 2
  162.     Endif
  163.  
  164. //    Print "Returning result ", Result
  165.  
  166.     StrXCmp = Result
  167. End Function
  168.