问题
What is the purpose of half- and full-width characters and what is the difference between them?
I am mostly curious because validator.js (an open-source string validation library) has a couple of functions that evaluate the form of a given input:
isFullWidth(str)
isHalfWidth(str)
isVariableWidth(str)
Why might someone want to evaluate the form of a some text?
Internally, the library uses this regex pattern to determine if the input is full-width:
/[^\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]/
and this pattern to determine if the input is half-width:
/[\u0020-\u007E\uFF61-\uFF9F\uFFA0-\uFFDC\uFFE8-\uFFEE0-9a-zA-Z]/
What is the significance of these code point ranges?
回答1:
They're used in Asian languages. Chinese and other Hanzi-based languages have the peculiar property of forming very grid-like blocks of text, since Hanzi/Kanji are square:
漢字,在中國亦称中文字、中国字、方塊字,是漢字文化圈廣泛使用的一種文字,也是世界上唯一仍被廣泛使用的高度發展的語素文字。廣義的漢字指從甲骨文、大篆、金文、籀文、小篆,至隶书、草书、楷书(以及衍生的行书)等的文字,狹義指以正楷作為標準寫法的漢字,也是今日普遍使用的現代漢字。漢字在古文中只稱「字」,少數民族為區別而稱「漢字」,指漢人使用的文字。
You'll notice that even punctuation marks preserve the column-like layout of characters. Interspersing some regular half-width or dynamic-width latin will destroy that:
漢字,在中國亦称中文字、中国字、方塊字,是漢字文化圈廣泛使用的一種文字,也是世界上唯一仍被廣泛使用的高度發展的語素文字。Here's some irregular latin. 廣義的漢字指從甲骨文、大篆、金文、籀文、小篆,至隶书、草书 (and here's some more)、楷书(以及衍生的行书)等的文字 (and some more),狹義指以正楷作為標準寫法的漢字,也是今日普遍使用的現代漢字。漢字在古文中只稱「字」,少數民族為區別而稱「漢字」,指漢人使用的文字。
Full-width latin characters are intended to preserve the block layout, by fitting into a Hanzi-sized square:
漢字,在中國亦称中文字、中国字、方塊字,是漢字文化圈廣泛使用的一種文字,也是世界上唯一仍被廣泛使用的高度發展的語素文字。Here's some regular latin.廣義的漢字指從甲骨文、大篆、金文、籀文、小篆,至隶书、草书、楷书(以及衍生的行书)等的文字,狹義指以正楷作為標準寫法的漢字,也是今日普遍使用的現代漢字。漢字在古文中只稱「字」,少數民族為區別而稱「漢字」,指漢人使用的文字。
Essentially, it just looks and reads better in this context.
来源:https://stackoverflow.com/questions/28120607/what-is-the-purpose-of-half-and-full-width-characters