写个函数判断一下(有BOM的自行处理):
- (defun Is_UTF8_No_BOM (str / is_UTF8 loop)
- (setq is_UTF8 t
- loop t
- )
- (while (and str loop)
- (cond ((< (car str) 128) (setq str (cdr str))) ;; 小于0x80为ACSCII字符
- ((< (car str) 192) (setq is_UTF8 nil loop nil)) ;;(11000000) 介于0x80和0xC0之间的为无效的UTF-8字符
- ((< (car str) 224) ;;此范围为2字节UTF-8字符
- (if (<= (length str) 1)
- (setq is_UTF8 nil loop nil)
- (if (/= (logand (cadr str) 192) 128) ;;_else (str[1] & 0xC0 != 0x80)
- (setq is_UTF8 nil loop nil) ;;不符合
- (setq str (cddr str)) ;;_else 继续
- )
- ) ;;if
- )
- ((< (car str) 240) ;;(11110000) 此范围为3字节UTF-8字符
- (if (<= (length str) 2)
- (setq is_UTF8 nil loop nil)
- (if (or (/= (logand (cadr str) 192) 128) (/= (logand (caddr str) 192) 128)) ;;_else
- (setq is_UTF8 nil loop nil) ;;不符合
- (setq str (cdddr str)) ;;_else 继续
- )
- )
- )
- (t (setq is_UTF8 nil loop nil))
- )
- )
- is_UTF8
- )
|