Attribute VB_Name = "StrFuncs"
' String functions
' Copyright (c) 1997-1998 by Desaware Inc.  All Rights Reserved

Option Explicit

' Search string sourcestring for first occurrance of any character in searchchars
Public Function StringSpan(sourcestring As String, searchchars As String) As Long
   Dim x&
   Dim strlen&
   Dim foundpos&
   For x = 1 To Len(sourcestring)
      If InStr(searchchars, Mid$(sourcestring, x, 1)) > 0 Then
         StringSpan = x
         Exit Function
      End If
   Next

End Function

' Search sourcestring for first character not found in searchchars
' Return 0 if entire string is in searchchars
Public Function StringSpan2(sourcestring As String, searchchars As String) As Long
   Dim x&
   Dim strlen&
   Dim foundpos&
   For x = 1 To Len(sourcestring)
      If InStr(searchchars, Mid$(sourcestring, x, 1)) = 0 Then
         StringSpan2 = x
         Exit Function
      End If
   Next
End Function

' Converts occurrances of HTML literal charcters to real characters
Public Function ConvertHtmlLiterals(strInput As String) As String
   Dim amppos&
   Dim semipos&
   Dim lit$
   Dim res$
   Dim charval As Integer
   amppos = InStr(strInput, "&")
   If amppos = 0 Then
      ' No literals, just copy
      ConvertHtmlLiterals = strInput
      Exit Function
   End If
   semipos = InStr(strInput, ";")
   If semipos = 0 Or semipos <= amppos + 1 Then
      ' No HTML literal string, just copy
      ConvertHtmlLiterals = strInput
      Exit Function
   End If
   ' Get the string
   lit$ = Mid$(strInput, amppos + 1, semipos - amppos - 1)
   If Left$(lit$, 1) = "#" Then
      ' It's a numeric literal
      charval = Val(Mid$(lit$, 2))
   Else
      Select Case LCase$(lit$)
         Case "lt"
            charval = 60
         Case "gt"
            charval = 62
         Case "amp"
            charval = 38
         Case "quot"
            charval = 34
         Case "emdash"
            charval = 151
         Case "copy"
            charval = 169
         Case "reg"
            charval = 174
         Case Else
            charval = 0
      End Select
   End If
   ' Yank out the unknown symbol
   If amppos > 1 Then
      res = Left$(strInput, amppos - 1)
   End If
   If charval <> 0 Then
      res = res & Chr$(charval)
   End If
   If semipos < Len(strInput) Then
      res = res & Mid$(strInput, semipos + 1)
   End If
   
   ' Why recursion? There may be more than one literal.
   ConvertHtmlLiterals = ConvertHtmlLiterals(res)
End Function

' Strip from the first line feed
Public Function StripLinefeeds(line$) As String
   Dim x%
   x% = InStr(line$, Chr$(10))
   If x = 0 Then StripLinefeeds = line Else StripLinefeeds = Left$(line$, x - 1)
End Function
