|
Strings: Nom propres avec exceptions |
Author(s) Jay Holovacs |
|
---Soumis par Jay Holovacs---
Nom propres avec exceptions.
Voici un ensemble de fonctions qui permettent de manipuler
certaines règles spéciales de noms propres. Étant modulaire, on peut ajouter
des règles supplémentaires, si cela est opportun.
Par exemple, en ce moment, les cas suivants sont couverts:
Henry VIII.
O'Hara
Tom McHill
Appeler la fonction en lui fournissant la chaîne à transformer, en
minuscules (l'argument original n'est pas modifié).
dim retval as string
retval=mixed_case("joe mcdonald")
Public Function mixed_case(str As Variant) As String
Dim ts As String, ps As Integer, char2 As String
If IsNull(str) Then
mixed_case = ""
Exit Function
End If
If Len(str) = 0 Then
mixed_case = ""
Exit Function
End If
ts = LCase$(str)
ps = 1
ps = first_letter(ts, ps)
special_name ts, 1
Mid$(ts, 1) = UCase$(left$(ts, 1))
If ps = 0 Then
mixed_case = ts
Exit Function
End If
While ps <> 0
If is_roman(ts, ps) = 0 Then
special_name ts, ps
Mid$(ts, ps) = UCase$(Mid$(ts, ps, 1))
End If
ps = first_letter(ts, ps)
Wend
mixed_case = ts
End Function
Private Sub special_name(str As String, ps As Integer)
Dim char2 As String
char2 = Mid$(str, ps, 2)
If (char2 = "mc" Or char2 = "o'") And Len(str) > ps + 1 Then Mid$(str, ps + 2) = UCase$(Mid$(str, ps + 2, 1))
End If
End Sub
Private Function first_letter(str As String, ps As Integer) As Integer
Dim p2 As Integer, s2 As String
s2 = str
p2 = InStr(ps, str, " ")
If p2 = 0 Then
first_letter = 0
Exit Function
End If
While is_alpha(Mid$(str, p2)) = False
p2 = p2 + 1
If p2 > Len(str) Then
first_letter = 0
Exit Function
End If
Wend
first_letter = p2
End Function
Public Function is_alpha(ch As String)
Dim c As Integer
c = Asc(ch)
Select Case c
Case 65 To 90
is_alpha = True
Case 97 To 122
is_alpha = True
Case Else
is_alpha = False
End Select
End Function
Private Function is_roman(str As String, ps As Integer) As Integer
Dim mx As Integer, p2 As Integer, flag As Integer, i As Integer
mx = Len(str)
p2 = InStr(ps, str, " ")
If p2 = 0 Then
p2 = mx + 1
End If
flag = 0
For i = ps To p2 - 1
If InStr("ivxIVX", Mid$(str, i, 1)) = 0 Then
flag = 1
End If
Next i
If flag Then
is_roman = 0
Exit Function
End If
Mid$(str, ps) = UCase$(Mid$(str, ps, p2 - ps))
is_roman = 1
End Function
|