如何拆分字符串,
程序集中的分隔符AT & T?拆分字符串程序集AT&T
这是我有字符串:6,3,1,2431,7,9,1221
欲1添加到所有的,
之间的数字。
的结果应该是:7, 4, 2432, 8, 10, 1222
我与ubuntu14工作 - 64位,Intel的CPU和GAS编译
如何拆分字符串,
程序集中的分隔符AT & T?拆分字符串程序集AT&T
这是我有字符串:6,3,1,2431,7,9,1221
欲1添加到所有的,
之间的数字。
的结果应该是:7, 4, 2432, 8, 10, 1222
我与ubuntu14工作 - 64位,Intel的CPU和GAS编译
伪代码:
; si = input string
di = output string
if 0 == [si] jmp NoNumberFound
NextNumberLoop:
rax = 0
ReadLoop:
bl = [si] & inc si
if 0 == bl jmp LastNumberFound
if ',' == bl jmp CommaFound
rax = rax*10 + (bl-'0') ; extend bl to 64b of course
jmp ReadLoop
CommaFound:
call StoreNumberPlusOne
[di]=',' & inc di
jmp NextNumberLoop
LastNumberFound:
call StoreNumberPlusOne
NoNumberFound:
[di]='0'
output resulting string and exit.
StoreNumberPlusOne:
inc rax (number+1)
print decimal formatted rax to [di] + make di point after it
ret
(DI/SI 64b上平台指针是当然rsi/rdi等等......它只是显示算法的伪代码,不能从字面上理解)
其他选项是在字符串本身进行分析而不用解析数字。
分配足够的缓存生成的字符串(如果你把输入作为n
次9
,输出缓冲器将几乎两倍只要输入缓冲器,包含n
次10
)。
将输入字符串复制到outputBuffer,并在其最后一个字符处具有指针ptr
。
doIncrement = true
while (outputBuffer <= ptr) {
if ',' == [ptr] {
if doIncrement {
move by 1 byte further everything from ptr+1 onward:
in ASM you can do that by simple REP MOVSB, but as the
source overlap destination, make sure you use STD (DF=1)
together with end pointers.
[ptr+1] = '1'
}
doIncrement = true
} else {
; [ptr] should be between '0' to '9' (for valid input)
if doIncrement {
++[ptr]
if '9'+1 == [ptr] {
[ptr] = '0'
} else {
doIncrement = false
}
}
}
--ptr;
}
因为x86-64保证有SSE2可用,所以我可能会倾向于使用'pcmpeqb' /'pmovmskb' /'bsf'来查找下一个','的位置。你甚至可以使用SSE作为string-> integer('pmaddwd',其矢量为1,10,100,... place-values)。如果SSSE3 pshufb可用,您可以做更多更酷的事情,例如[将IPv4虚线四字符串转换为整数](http://stackoverflow.com/a/31683632/224132)。 –
要做到这一点,最好的方法是编写一些代码,扫描字符串中的逗号,将ASCII码转换为数字值并添加任何你想要的。如果您要求我们为您编写此代码,那么您就错了。 –
源文件中是否用逗号分隔“字符串”?通过指针或堆栈?字符串元素的大小和类型?你有什么尝试? – jolati