其实按照楼主的本意
本来使用change、sed等文本替换工具高效率、高兼容的
但不知什么原因
楼主一定要纯P的方案
只好折中了一下
使用ASCII Assembler生成一个文本替换工具
然后再用它替换源文本
至于它是否纯P
就不用讨论太多了
------------------------------------------------------------------
关于ASCII Assembler的细节搜索一下过去的帖子(按内容)
下面是sbs2的用法说明与源码
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Usage: sbs2.com NUMBER "STRING1" "STRING2" <infile >outfile
::
:: infile and outfile must be files (to allow random access), so in
:: NT/W2000/XP no pipe (program1 | sbst | program2) must be used!
:: infile and outfile must not be the same file!
::
:: Substitutes the NUMBER ocurrence of STRING1 in infile by
:: STRING2 and writes the result to outfile
::
:: You can include any character in STRING1/2 by using its
:: hex value (e.g. $0d for <CR> or $1a for EOF)
::
:: If NUMBER = 0 all STRING1 are substituted by STRING2
::
:: If an error is detected or nothing is substituted, erorrlevel=0
:: replace the original file only if errorlevel>=1 (=number of
:: substitutions).
::
:: Instead of the double quotes (") you can also use single quotes (')
:: with a different meaning for string2:
:: "string2" : normal substitution
:: 'string2" : before substitution the output file is rewinded
:: "string2' : after substitution the outputfile is closed
some examples:
-----------------------------------------------------
Substitutes the 2. occurrence of user by Susan
-----------------------------------------------------
sbs2.com 2 "user" "Susan" <%1 >_._
if errorlevel 1 copy _._ %1
del _._
-----------------------------------------------------
Converts dos textfiles to unix textfiles
-----------------------------------------------------
sbs2.com 0 "$0d" "" <%1 >_._
if errorlevel 1 copy _._ %1
del _._
-----------------------------------------------------
Converts unix textfiles to dos textfiles
-----------------------------------------------------
sbs2.com 0 "$0a" "$0d$0a" <%1 >_._
if errorlevel 1 copy _._ %1
del _._
-----------------------------------------------------
echo without CRLF
-----------------------------------------------------
echo set a=|sbs2.com 0 "$0d$0a" "" >_.bat
-----------------------------------------------------
Remove trailing blanks from all lines
-----------------------------------------------------
:start
sbs2.com 0 " $0d" "$0d" <%1 >_._
if not errorlevel 1 goto ende
copy _._ %1
goto start
:ende
del _._
-----------------------------------------------------
Remove all single CR LF (ascii text file -> WORD)
-----------------------------------------------------
sbs2.com 0 "$0d$0a$0d$0a" "$0a" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 0 "$0d$0a" "" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 0 "$0a" "$0d$0a" <%1 >_._
if errorlevel 1 copy _._ %1
del _._
-----------------------------------------------------
Extract line 5-9 from a text file
-----------------------------------------------------
sbs2.com 4 "$0d$0a" '" <%1 >_._
if errorlevel 1 copy _._ %1
sbs2.com 5 "$0d$0a" "$0d$0a' <%1 >_._
if errorlevel 1 copy _._ %1
-----------------------------------------------------
The source code
-----------------------------------------------------
@=$100
move.w #$81,r5
movu.bw -1.b(r5.w),r2
eor.l r0,r0
eor.l r4,r4
.lab1:
_10: dec.w r2
bmi.w x100
move.b (r5.w),r0
inc.w r5
cmp.b #'0',r0
blo.b _20
cmp.b #'9',r0
bhi.b _20
sub.b #'0',r0
mulsq.l #10,r4,r4
add.l r0,r4
move.w #_30,lab1
br.b _10
_20: jmp.w (lab1)
_30: move.l r4,number
move.w #buf1,r6
bsr.w get_string
or.w r6,r6
beq.b .x100
move.w #buf2,r6
bsr.w get_string
eor.l r5,r5
_a00: move.w #buf1,r6
eor.w r4,r4
_a10: move.b #$3f,m0
move.w #buf0,r1
move.w #1,r2
eor.w r3,r3
trap #$21
bcs.b .x100
or.w r0,r0
beq.b _x100
move.b buf0,r0
cmp.b (r6.w),r0
beq.b _a20
_a50: or.w r4,r4
beq.b _a30
move.w #$4201,r0
move.w #-1,r2
move.w r4,r1
neg.w r1
trap #$21
move.w #buf1,r1
move.w #1,r2
_a30: move.b #$40,m0
inc.w r3
trap #$21
bcs.b .x100
cmp.w #buf2,r1
bne.b _a60
tst.b #1,stop
bne.b _x110
_a60: cmp.w r0,r2
beq.b _a00
.x100: br.b x100
_a20: inc.w r6
inc.w r4
cmp.w buf1l,r4
blo.b _a10
inc.l r5
move.l number,r0
or.l r0,r0
beq.b _a40
cmp.l r0,r5
beq.b _a40
dec.w r4
move.w #buf1,r1
move.w #1,r2
br.b _a50
_a40:
tst.b #1,start
beq.b _a41
move.w #$4200,r0
eor.w r1,r1
eor.w r2,r2
inc.w r3
trap #$21
move.b #$40,m0
trap #$21
dec.w r3
_a41: move.w #buf2,r1
move.w buf2l,r2
br.b _a30
_x100: or.w r4,r4
beq.b _x110
move.w #buf1,r1
move.w r4,r2
br.b _a30
_x110:
move.w r5,r0
or.l r5,r5
beq.b x101
move.l number,r1
or.l r1,r1
beq.b _x120
cmp.l r5,r1
bhi.b x100
move.b #1,r0
br.b x101
_x120: cmp.l #$ff,r5
bls.b x101
move.b #-1,r0
br.b x101
x100: eor.b r0,r0
x101: move.b #$4c,m0
trap #$21
get_string:
eor.w r1,r1
move.w r6,r3
move.w #_10,lab1
move.w #_30,lab2
; move.l #(_30<<16)+_10,lab1 ; kuerzer
_10: dec.w r2
bmi.b _x100
move.b (r5.w),r0
inc.w r5
eor.b m0,m0
cmp.b #'"',r0
beq.b _20
cmp.b #"'",r0
beq.b _21
jmp.w (lab1)
_21: inc.b m0
_20: jmp.w (lab2)
_30: move.w #_40,lab1
move.w #_50,lab2
; move.l #(_50<<16)+_40,lab1 ; kuerzer
move.b m0,start
br.b _10
_40: or.b r1,r1
bne.b _41
cmp.b #'$',r0
bne.b _42
move.w #2,r1
br.b _10
_42: move.b r0,(r6.w)
inc.w r6
br.b _10
_41: or.b #$20,r0
sub.b #'a'-10,r0
bpl.b _43
add.b #'a'-10-'0',r0
_43: lsl.b #4,m1
add.b m1,r0
dec.b r1
beq.b _42
move.b r0,m1
br.b _10
_50: sub.w r3,r6
move.w r6,-2.b(r3.w)
move.b m0,stop
rts.w
_x100: addq.w #2,r7
br.b x100
lab1: dc.w .lab1
lab2: blk.w 1
even
start: blk.b 1
stop: blk.b 1
number: blk.l 1
buf0: blk.w 1
buf1l: blk.w 1
buf1: blk.w 200
buf2l: blk.w 1
buf2: blk.w 200