root/lang/vbscript/Formatter/Formatter.vbs @ 14556

Revision 236, 7.0 kB (checked in by mayuki, 6 years ago)

vbscript/Formatter: miss

  • Property svn:keywords set to Id
Line 
1Option Explicit
2
3'/*
4' * Formatter.vbs
5' * $Id$
6' *
7' * Copyright (C) 2005 Mayuki Sawatari <mayuki@misuzilla.org>
8' *      All rights reserved.
9' *
10' * Redistribution and use in source and binary forms, with or without
11' * modification, are permitted provided that the following conditions
12' * are met:
13' * 1. Redistributions of source code must retain the above copyright
14' *    notice, this list of conditions and the following disclaimer.
15' * 2. Redistributions in binary form must reproduce the above copyright
16' *    notice, this list of conditions and the following disclaimer in the
17' *    documentation and/or other materials provided with the distribution.
18' *
19' * THIS SOFTWARE IS PROVIDED BY THE MISUZILLA.ORG ``AS IS'' AND
20' * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21' * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22' * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23' * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24' * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25' * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26' * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27' * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28' * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29' * SUCH DAMAGE.
30' */
31
32'// �N���X������^�C�v�B
33Function StringFormat(sFormat, oParam)
34Dim oFormatter: Set oFormatter = New StringFormatter
35Dim oFormatHandler: Set oFormatHandler = New ParamFormatHandler
36        StringFormat = oFormatter.Format(sFormat, oParam, oFormatHandler)
37End Function
38
39Function StringFormatEval(sFormat)
40Dim oFormatter: Set oFormatter = New StringFormatter
41Dim oFormatHandler: Set oFormatHandler = New EvalFormatHandler
42        StringFormatEval = oFormatter.Format(sFormat, Empty, oFormatHandler)
43End Function
44
45' =============================================================================
46
47Class StringFormatter
48        Private oRegexFormatCapture
49        Private oRegexEscape
50
51        '// Ctor
52        Private Sub Class_Initialize()
53                Set oRegexFormatCapture = New RegExp
54                oRegexFormatCapture.Global = True
55                oRegexFormatCapture.Pattern = "(?=^|[^\\]|.{0})\{(.*?[^\\]|)\}"
56
57                Set oRegexEscape = New RegExp
58                oRegexEscape.Global = True
59                oRegexEscape.Pattern = "\\([{}\\])"
60        End Sub
61       
62        '//
63        '// Format - ��������`����
64        '//
65        '// sFormat: �t�H�[�}�b�g������'// oParams: �t�H�[�}�b�g�n���h�����p�����[�^��v�Ƃ������ȏꍇ�̓p�����[�^�z��w�������O��mpty
66        '// oFormatHandler: �t�H�[�}�b�g�n���h��
67        Public Function Format(sFormat, oParam, oFormatHandler)
68                Dim oMatches
69                Dim oMatch
70                Dim sNewString
71                Dim lStartPos
72                Dim sExpressionResult
73               
74                Set oMatches = oRegexFormatCapture.Execute(sFormat)
75               
76                'Call WScript.StdOut.WriteLine("")
77                'Call WScript.StdOut.WriteLine("Format: " & sFormat)
78                'Call WScript.StdOut.WriteLine(String(Len("Format: " & sFormat), "="))
79
80                lStartPos = 1
81               
82                For Each oMatch In oMatches
83                       
84                        sExpressionResult =  oRegexEscape.Replace( _
85                                oFormatHandler.Expression(oMatch.SubMatches(0), oParam), "$1")
86
87                        If (oMatch.FirstIndex - lStartPos + 1 > 0) Then
88                                sNewString = sNewString & oRegexEscape.Replace( _
89                                        Mid(sFormat, lStartPos, oMatch.FirstIndex - lStartPos + 1), "$1") & sExpressionResult
90                                'Call WScript.StdOut.WriteLine("Append String: " & Mid(sFormat, lStartPos, oMatch.FirstIndex - lStartPos + 1))
91                        Else
92                                sNewString = sNewString & sExpressionResult
93                        End If
94
95                        'Call WScript.StdOut.WriteLine("New String: " & sNewString)
96                        'Call WScript.StdOut.WriteLine("String start pos: " & lStartPos)
97                        'Call WScript.StdOut.WriteLine("Match pos: " & oMatch.FirstIndex)
98                        'Call WScript.StdOut.WriteLine("Next start pos: " & oMatch.FirstIndex + oMatch.Length + 1)
99                        'Call WScript.StdOut.WriteLine("Value: " & oMatch.Value)
100                        'Call WScript.StdOut.WriteLine("SubMatches: " & oMatch.SubMatches(0))
101                        'Call WScript.StdOut.WriteLine("Exec Expression: " & sExpressionResult)
102                        'Call WScript.StdOut.WriteLine("")
103                       
104                        lStartPos = oMatch.FirstIndex + oMatch.Length + 1
105                Next
106               
107                '// �c��                If (lStartPos < Len(sFormat)) Then
108                        'Call WScript.StdOut.WriteLine("String start pos: " & lStartPos)
109                        'Call WScript.StdOut.WriteLine("String Len: " & Len(sFormat))
110                        'Call WScript.StdOut.WriteLine("Append String: " & Mid(sFormat, lStartPos, Len(sFormat) - lStartPos + 1))
111                        sNewString = sNewString & oRegexEscape.Replace( _
112                                Mid(sFormat, lStartPos, Len(sFormat) - lStartPos + 1), "$1")
113                End If
114               
115                Format = sNewString
116        End Function
117End Class
118
119
120'/*
121' * �t�H�[�}�b�g��ɂ傲�ɂ傷�����h��
122' *
123' * Public Function Expression(sFormatParam, oParam) 'As String
124' * �߂��Œu����������
125' */
126
127'// EvalFormatHandler - Eval�����]���ɂ���
128'//     �p�����[�^�͗v�����B
129'//     ex. '{String(5, "=")}' => '======'
130Class EvalFormatHandler
131        Public Function Expression(strFormatParam, oParam)
132                On Error Resume Next
133                Expression = vbNullString
134                Expression = Eval(strFormatParam)
135        End Function
136End Class
137
138'// DictionaryFormatHandler - �p�����[�^�ɓn���ꂽ Dictionary �����[�Œl�������
139'//     ex. oDic.Add("key", "value")
140'//                     '{key}' => 'value'
141Class DictionaryFormatHandler
142        Public Function Expression(strFormatParam, oParam)
143                On Error Resume Next
144                Expression = vbNullString
145                ' �^�`�F�b�N�����ق�������?
146                Expression = oParam.Item(strFormatParam)
147        End Function
148End Class
149
150'// UrlQueryFormatHandler - �p�����[�^�ɓn���ꂽ Dictionary �����[�Œl������A
151'//             &query=value �̌`�ɂ����A���l�����݂��Ȃ�������̂܂܂ɂ���
152'//             '{query:key}' => '&query=value'
153'//             '{query:not-exists-key}' => ''
154Class UrlQueryFormatHandler
155        Public Function Expression(strFormatParam, oParam)
156                Dim sValue
157                Dim sTemp: sTemp = Split(strFormatParam, ":", 2)
158               
159                On Error Resume Next
160                Expression = vbNullString
161                If UBound(sTemp) = 1 And oParam.Exists(sTemp(1)) Then
162                        sValue = oParam.Item(sTemp(1))
163                        If sValue <> "" Then
164                                Expression = "&" & sTemp(0) & "=" & oParam.Item(sTemp(1))
165                        End If
166                End If
167        End Function
168End Class
169
170'// ParamFormatHandler - �p�����[�^��������`����
171'// {n} ������{n:format}
172'//     format:
173'//             x: 16�i��(������)�B
174'//     ex:
175'//             oParam(0) = "hoge" :  '{0}' => 'hoge'
176'//             oParam(1) = 10     :  '{1:x}' => 'a'
177Class ParamFormatHandler
178        Public Function Expression(sFormatParam, oParam)
179                'On Error Resume Next
180                Expression = vbNullString
181
182                Dim sFormat: sFormat = ""
183                Dim sIndex: sIndex = sFormatParam
184                Dim sTemp: sTemp = Split(sFormatParam, ":", 2)
185                If UBound(sTemp) = 1 Then
186                        sIndex = sTemp(0)
187                        sFormat = sTemp(1)
188                End If
189               
190                If (IsNumeric(sIndex)) Then
191                        Dim lIndex: lIndex = CLng(sIndex)
192                        If UBound(oParam) >= lIndex Then
193                                If sFormat <> "" Then
194                                        Select Case sFormat
195                                        Case "x"
196                                                Expression = LCase(Hex(oParam(lIndex)))
197                                        Case Else
198                                        End Select
199                                Else
200                                        Expression = oParam(lIndex)
201                                End If
202                        End If
203                End If
204               
205        End Function
206End Class
Note: See TracBrowser for help on using the browser.