ab.com コミュニティ https://www.activebasic.com/forum/ |
|
範囲判定と文字用マクロ https://www.activebasic.com/forum/viewtopic.php?t=261 |
ページ 1 / 1 |
作成者: | 高信期 [ 2005年8月08日(月) 01:55 ] |
記事の件名: | 範囲判定と文字用マクロ |
コード: Const InRangeLen(Value, AMin, Length) = LOWORD((Value) - (AMin)) < (Length) Const InRange(Value, AMin, AMax) = InRangeLen(Value, AMin, (AMax) - (AMin) + 1) Const GetLowerFlag(Result) = (Result) And &H20 Const IsByteDigit(Ch) = InRangeLen(Ch, &H30, &H0A) Const IsByteUpper(Ch) = InRangeLen(Ch, &H41, &H1A) Const IsByteLower(Ch) = InRangeLen(Ch, &H61, &H1A) Const IsByteAlpha(Ch) = IsByteLower((Ch) Or &H20) Const IsByteAlphaNumeric(Ch) = IsByteDigit(Ch) Or IsByteAlpha(Ch) Const ByteUpper(Ch) = (Ch) Xor GetLowerFlag(IsByteLower(Ch)) Const ByteLower(Ch) = (Ch) Or GetLowerFlag(IsByteUpper(Ch)) Const SameTextChar(Ch1, Ch2) = ((Ch1) Xor (Ch2)) = GetLowerFlag(IsByteLower(Ch1) Xor IsByteLower(Ch2)) Const InSJISLeadBytes(Ch) = InRangeLen((Ch) Xor &H20, &HA1, &H3C) Const InSJISTrailBytes(Ch) = InRangeLen(Ch, &H40, &HBD) And ((Ch) <> &H7F)InRangeLen 整数型の値Valueが0~65535以内のAMinよりLength個の範囲にあるかを判定する。 InRange 整数型の値Valueが0~65535以内のAMin以上AMax以下の範囲にあるかどうかを判定する。 GetLowerFlag Resultが真(-1)であれば&H20、偽(0)であれば0を返すヘルパーマクロ。 IsByteDigit 文字Chが数字であるかを判定する。 IsByteUpper~IsByteAlphaNumeric API関数IsCharUpper~IsCharAlphaNumericをマクロにしたもの。 ByteUpper,ByteLower API関数CharUpper,CharLowerをマクロにしたもの。ただし単一文字限定。 SameTextChar 文字Ch1と文字Ch2が同じ文字であるかを大/小文字の区別をせず判定する InSJISLeadBytes(Ch) 文字ChがShift-JISの全角文字の1バイト目にあたるかを判定する。 InSJISTrailBytes(Ch) 文字ChがShift-JISの全角文字の2バイト目になりえるかを判定する。 マクロは取り扱いが難しいものです。 上記のマクロの引数に実数型や文字列型の変数を利用すると見事に破綻するはずです。 また、引数に関数の戻り値を利用するのも危険が伴います。 ただし、うまく使うことにより関数より高速に処理出来ます。 例えば、InSJISLeadBytesの処理時間はIsDBCSLeadByte関数の1/5以下です。 以下は上記のマクロを使ったサンプルです。 コード: #prompt '文字列にあるアルファベットをすべて大文字にして返す Function UCase(Str As String) As String Dim Length As Long, I As Long Length = Len(Str) UCase = ZeroString(Length) For I = 0 To Length - 1 UCase = ByteUpper(Str) Next I End Function '文字数を数える(全・半角の区別はしない) Function JLen(S As String) As Long Dim I = 0 As Long JLen = 0 While TRUE If InSJISLeadBytes(S) Then I = I + 2 ElseIf S <> 0 Then I = I + 1 Else Exit Function End If JLen = JLen + 1 Wend End Function Dim A As String Print UCase("abcdef12345アイウエオGHIJKLあいうえおmnopqr") Print JLen("アイウエオあいうえお") Input A End |
ページ 1 / 1 | 全ての表示時間は UTC+09:00 です |
Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |