05/19 指定文字列から、指定ペア文字で囲まれる部分を削除。前方からスキャンして削除実行
指定文字列に対して、指定ペア文字列で囲まれる部分を削除するAppleScriptです。
この手の処理では、文字列の前と後ろからサーチするような処理が多いですが、本プログラムはどちらも前方からサーチします。
プログラムのタイトルを見ただけでは何に使ったものかさっぱり分かりませんが、プログラムリストを見ると一目瞭然。CocoaのAPIの名前をAppleScriptObjCに自動置換するための試作品です(ものすごく強引で、やっつけ仕事のオンパレードなうえに、なんでもかんでも処理できるようにはなっていません)。
もうちょっとこねくり回すとなんとかなってくるものでしょうか。
| スクリプト名:指定文字列から、指定ペア文字で囲まれる部分を削除。前方からスキャンして削除実行 |
| –Cocoaのメソッド名をAppleScriptObjCフォーマットに変換するテスト
–set aStr to “getColorR:(float *)r G:(float *)g B:(float *)b atIndex:(int)i ofSimulationType:(int)type;” set aStr to “drawAtPoint:(CGPoint)point forWidth:(CGFloat)width withFont:(UIFont *)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat *)actualFontSize lineBreakMode:(UILineBreakMode)lineBreakMode baselineAdjustment:(UIBaselineAdjustment)baselineAdjustment” set sStr to “(” set eStr to “)” –変数の型で2語に別れるものを徹底的に置換 set aStr to repChar(aStr, “unsigned short”, “unsignedshort”) of me set aStr to repChar(aStr, “signed short”, “signedshort”) of me set aStr to repChar(aStr, “signed int”, “signedint”) of me set aStr to repChar(aStr, “unsigned int”, “unsignedint”) of me if aStr does not end with “;” then set aStr to aStr & “;” end if set aStr to trimStrByCharPair(aStr, sStr, eStr, 0, 0) of me set aStr to repChar(aStr, tab, “”) of me set aStr to repChar(aStr, character id 10, “”) of me set aStr to repChar(aStr, “;”, ” “) of me set aStr to repChar(aStr, “:”, “_”) of me set bList to parseByDelim(aStr, {“_”, ” “}) of me set aRes to “” repeat with i from 1 to ((length of bList) - 1) by 2 set j to contents of item i of bList set aRes to aRes & (j & “_”) end repeat set bRes to “(” repeat with i from 2 to (length of bList) by 2 set j to contents of item i of bList set bRes to bRes & (j & “, “) end repeat set bRes to bRes & “)” set bRes to repChar(bRes, “, )”, “)”) of me –ゴミ掃除 set cRes to aRes & bRes –>”drawAtPoint_forWidth_withFont_minFontSize_actualFontSize_lineBreakMode_baselineAdjustment_(point, width, font, minFontSize, actualFontSize, lineBreakMode, baselineAdjustment)” –指定文字列から、指定ペア文字で囲まれる部分を削除。前方からスキャンして削除実行 on trimStrByCharPair(aStr, sStr, eStr, sTrimOffset, eTrimOffset) repeat set sOffst to offset of sStr in aStr set eOffst to offset of eStr in aStr set aLen to length of aStr if aLen < 2 then return “” if sOffst = 0 or eOffst = 0 then exit repeat end if if sOffst > 1 then set tmp1Str to text 1 thru (sOffst - 1 + sTrimOffset) of aStr set tmp2Str to text (eOffst + 1 + eTrimOffset) thru -1 of aStr set aStr to tmp1Str & tmp2Str else if sOffst = 1 then set aStr to text (eOffst + 1) thru -1 of aStr else if sOffst = aLen - 1 then set aStr to “” end if end repeat return aStr end trimStrByCharPair –文字置換ルーチン on repChar(origText, targStr, repStr) set {txdl, AppleScript’s text item delimiters} to {AppleScript’s text item delimiters, targStr} set temp to text items of origText set AppleScript’s text item delimiters to repStr set res to temp as text set AppleScript’s text item delimiters to txdl return res end repChar –与えられた文字列を、指定デリミタ文字でparseしてリストにして返す on parseByDelim(aData, aDelim) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to aDelim set dList to text items of aData set AppleScript’s text item delimiters to curDelim return dList end parseByDelim |
































