-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFormatMessage.rvb
57 lines (55 loc) · 2.08 KB
/
FormatMessage.rvb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' FormatMessage.rvb -- May 2012
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: FormatMessage
' Purpose: Slot-based string formatting function.
' Example:
' Dim str
' str = FormatMessage ("Hello, Mr. %1%, today is %2%.", Array("McNeel", Date))
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FormatMessage(strMessage, arrArguments)
Dim strResult, i
strResult = strMessage
For i = 0 To UBound(arrArguments)
strResult = Replace(strResult, "%" & CStr(i+1) & "%", CStr(arrArguments(i)))
Next
strResult = Replace(strResult, "\n", VbCrLf)
strResult = Replace(strResult, "\t", vbTab)
FormatMessage = strResult
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Function: Printf
' Purpose: Works like the printf-function in C/C++.
' Arguments: A string with format characters, and an array to expand.
' The format characters are always "%x", independent of the type.
' Example:
' Dim str
' str = Printf("Hello, Mr. %x, today is %x.", Array("McNeel", Date))
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Printf(strMessage, arrArguments)
Dim strResult, intPosition, i
strResult = ""
intPosition = 0
For i = 1 To Len(strMessage)
If Mid(strMessage, i, 1) = "%" Then
If i < Len(strMessage) Then
If Mid(strMessage, i + 1, 1) = "%" Then
strResult = strResult & "%"
i = i + 1
ElseIf Mid(strMessage, i + 1, 1) = "x" Then
strResult = strResult & CStr(arrArguments(intPosition))
intPosition = intPosition + 1
i = i + 1
End If
End If
Else
strResult = strResult & Mid(strMessage, i, 1)
End If
Next
Printf = strResult
End Function