EmEditorみんなでまとめサイト

Urlデコードマクロ

最終更新:

匿名ユーザー

- view
だれでも歓迎! 編集
ActivePointX = document.selection.GetActivePointX(eePosView)
ActivePointY = document.selection.GetActivePointY(eePosView)
AnchorPointX = document.selection.GetAnchorPointX(eePosView)
AnchorPointY = document.selection.GetAnchorPointY(eePosView)

'選択範囲の端点とカーソル位置のどちらが小さい(左上)か
bGreater = document.selection.IsActiveEndGreater

result = UrlDecode(document.selection.text)
document.selection.text = result

if bGreater then
    document.selection.SetAnchorPoint eePosView, AnchorPointX, AnchorPointY
else
    document.selection.SetAnchorPoint eePosView, ActivePointX, ActivePointY
end if

Function UrlDecode(strEncoded)

 Dim lngLength                                                          '文字列の長さを格納する
 Dim strSingle                                                        '抜き出した 1 文字を格納する
 Dim lngReadCount                                                     '文字列読み込み位置カウンタ
 Dim lngAsc                                                             '1 文字分の文字コードを格納
 Dim BCode
 
    lngLength = Len(strEncoded)                                                 'URL エンコードされている文字列の長さを得る
    If Not CBool(lngLength) Then Exit Function                                  '0 文字の場合、関数を抜ける
    ReDim bytResult(lngLength - 1)                                              'デコード結果格納バッファ領域を確保
    lngReadCount = 1                                                            '読み込みカウンタは 1 から開始
    BCode = 0
    Do                                                                          '文字列の終端までループ
        strSingle = Mid(strEncoded, lngReadCount, 1)                           '1 文字を抜き出す
        strHex = "&H"                                                             '16 進表記文字コードを格納する領域を確保
        If strSingle = "%" Then                                                 '"%" であった場合
            ret = PersentDecode(Mid(strEncoded, lngReadCount + 1, 2), lngReadCount)
            if BCode = 0 and ret >= 128 then
                BCode = ret
            else
                UrlDecode = UrlDecode & Chr(BCode*256+ret)
                BCode = 0
            end if
        ElseIf strSingle = "+" Then                                             '"+" であった場合
            UrlDecode = UrlDecode & " "                                     '半角スペース(" ")を代わりに入れる
            lngReadCount = lngReadCount + 1                                     '読み込みカウンタをインクリメント
        Else                                                                    'その他の文字であった場合
            lngAsc = CLng(Asc(strSingle)) And &HFFFF&                           '文字コードを符号無し長整数型(嘘)にキャスト
            If lngAsc <= &HFF& Then                                             '"&HFF" 以下であった場合
                UrlDecode = UrlDecode & Chr(lngAsc)                        'バイト型にキャストし、配列に代入
                lngReadCount = lngReadCount + 1                                 '読み込みカウンタをインクリメント
            Else                                                                'その他の文字(マルチバイト文字)の場合
                lngReadCount = lngReadCount + 1                                 '読み込みカウンタをインクリメント
            End If
        End If
    Loop Until lngReadCount > lngLength
    
End Function

Function PersentDecode(str, byref rc)
    for i = Len(str) to 1 step -1
        str = left(str, i)
        strHex = "&H" & str
        If IsNumeric(strHex) Then
            PersentDecode = CByte(strHex)
            rc = rc + i + 1
            exit function
        End If
    next
    PersentDecode = &H25
    rc = rc + 1
End Function
ウィキ募集バナー