本帖最后由 llsheng_73 于 2022-12-13 15:57 编辑
这样查找字符串的话,双字节字符会出现问题,
原因是因为双字节字符可能会被从高位和低位拆开
(VL-STRING->LIST"汉字测试")
(186 186 215 214 178 226 202 212)
(vl-list->string'(214 178 226 202))
"植馐"
结果会导致(SYZ-FIND-STR1 1"汉字测试""植馐")返回4
个人觉得应该先把字符串分解成单字符表,然后从表中查找,才能避免此类尴尬,不清楚高版本下有没此类尴尬
- (defun explodestr(str / a c);;
- (setq a(vl-string->list str))
- (while a
- (if(<(car a)129)(setq c(cons(chr(car a))c)a(cdr a))
- (setq c(cons(strcat(chr(car a))(chr(cadr a)))c)a(cddr a))))
- (reverse c))
- (defun tt(n l1 l2 fuz / a i f m);;;从大表中指定位置开始找小表首次出现的位置
- (repeat n(setq l1(cdr l1)))
- (setq a(car l2)m(length l2))
- (while(and(not f)(setq i(vl-position a l1)))
- (setq f(vl-every(function(lambda(x y)(setq l1(cdr l1))(equal x y fuz)))l2(repeat i(setq l1(cdr l1))))
- n(+ i n m)))
- (if f(- n m)))
- (defun find-str(start dstr1 sstr2 / l1 n )
- (if(setq l1(explodestr dstr1)n 0 start(tt start l1(explodestr sstr2)0))
- (repeat start(setq n(+(strlen(car l1))n)l1(cdr l1))))
- n)
(FIND-STR 1"汉字测试""植馐")nil
(FIND-STR 1"汉字测试""测试")4
|