autlisp Big5/ UTF-8 字碼 中文字字元數的計算

1. 前言

在 Autocad 2022 之前的 autolisp 函數 (strlen "字串") 它會返回字串的字元數,但是有一個特殊現象是它會把中文字的字元數以 1 個中文字元返回字元數為 2。 然而在 Autocad 2022 以後,和 BricsCAD 相同,其設定為一個中文字為一個字元。這也許是 Autocad 2022 年之後正式支援 UTF-8 字碼輸出有關。想當年編寫 autolisp 要顯示中文時還特定將檔案文字編碼指定成 Big5 碼,以避免載入 Autocad 時產生亂碼。我們也一直質疑為什麼 autocad 不支援 UTF-8 的檔案。

;;; autocad 2022 之前
指令:(strlen "中文")
4

;;; autocad 2022 後及 BricsCAD
指令:(strlen "中文")
2

現在朋友遇到了一個問題,那就是他的客戶有許多程式是需要使用 (stelen …) 來計算字串的字元數的,但是在 Autocad 2022 後,其 autolisp 程式在應用上會造成與之前版本的工具有所偏差。以致於無法引入 Autocad 2022 或是 BricsCAD 等工具。 以下程式可以先解燃眉之急,但是以長遠來看,未來還是要檢示一下程式中是否要修正的地方。

2. 原始碼

這個程式是以中文字與英文字及符號為區分字元數。希望以 (cs-to-strlen "字串") 取代僅以 (strlen "字串") 去判斷返回的字元數。然而這個程式無法如 (strlen …) 接受不同型式的引數;如 (strlen)、(strlen "字串1") 或 (strlen "字串1" "字串2" "字串3")等等。 也就是說它只能使用一個引數。例子如下

;;; autocad / bricscad
指令:(cs-to-strlen "中文12345")
9
指令:(cs-to-strlen "中文 abcd")
9
指令:(cs-to-strlen "123中文 abcd")
12

如果以 (cs-to-strlen …) 取代 (strlen …) 修改程式,並且注意引數的型式,它可以支援以前的程式,無論 CAD 軟體是否有支援 UTF-8 字碼。(cs-to-strlen "字串")可以返回舊有程式中計算字串 1 中文字 2 字元數的數值。 現階段已經在 AutoCAD 2017/ AutoCAD 2022/ AutoCAD 2024 及 BricsCAD 2023 測試可行。

2.1. cs-to-strlen.lsp

;;;; -*- encoding:big5 -*-
;; autocad 2022 (Big5/ UTF-8) 中文字字元數的計算

;;; 重載原始碼
(defun reload-file (filename)
  (load filename)
  (print (strcat filename " 已讀取!"))
  (princ))

(defun c:rrr-cs-to-strlen ()
  (reload-file "cs-to-strlen.lsp"))

(defun cs-to-strlen (str / num fig1 sum)
  ;; Big5 中字字位計算
  (if (> (strlen "中") 1)
      (progn
        ;; (print ">1")
        (strlen str))
      (progn
        (setq num (strlen str)
              fing1 0
              sum 0)
        ;; (print num)
        (while (< 0 num)
               (if (> (ascii (substr str 1 1)) 128)
                   (setq sum (+ sum 2))
                   (setq sum (1+ sum)))
               (setq str (substr str 2))
               (setq num (1- num)))
        sum)))

2.2. 版權宣告/ Copyright notice

Copyright (C) 2023 Each Application Mechanical Design Studio

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3. 各申機械設計工作室/ Each Application Mechanical Design Studio