Skip to content

bgever/HalfFullWidth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HalfFullWidth

A .NET Standard library to convert between halfwidth and fullwidth Unicode forms.

Installation

Install the HalfFullWidth package from NuGet.

https://www.nuget.org/packages/HalfFullWidth/

Usage

Import the HalfFullWidth namespace to get access to the extension methods on String and Char:

using HalfFullWidth;
// ...
string fullwidthString = "ABC".ToFullwidthString(); // ABC
string halfwidthString = "ABC".ToHalfwidthString(); // ABC
char fullwidthChar = 'A'.ToFullwidthChar(); // A
char halfwidthChar = 'A'.ToHalfwidthChar(); // A

Or use the static conversion methods explicitly:

string fullwidthString = HalfFullWidth.Convert.ToFullwidthString("ABC"); // ABC
string halfwidthString = HalfFullWidth.Convert.ToHalfwidthString("ABC"); // ABC
char fullwidthChar = HalfFullWidth.Convert.ToFullwidthChar('A'); // A
char halfwidthChar = HalfFullWidth.Convert.ToHalfwidthChar('A'); // A

Supported frameworks

The library targets both .NET Standard 1.3 and .NET Standard 2.0.

Purpose

This library provides conversion between halfwidth and fullwidth Unicode forms, what is not readily available with .NET Core. Thus offering a cross-platform alternative to the native Windows APIs (see Alternatives).

Conversion is useful for situations where you need to handle Unicode text that can be represented in both halfwidth and fullwidth forms, commonly used in Chinese, Japanese and Korean language scripts.

  • Chinese: 全形和半形、「fullwidth」的各地常用別名(全角、全形)、「halfwidth」的各地常用別名(半角、半形)
  • Japanese: 全角と半角、 半角カナ(はんかくカナ)、半角片仮名(はんかくかたかな, Halfwidth Katakana, 半形假名)
  • Korean: 전각 문자와 반각 문자. 전각/반각 모양

For example, in halfwidth kana (半角カナ) the below halfwidth Japanese alphabet would be converted into the below fullwidth representation.

Halfwidth:

   ア a    イ i    ウ u    エ e    オ o
K  カ ka   キ ki   ク ku   ケ ke   コ ko   キャ kya  キュ kyu  キョ kyo
S  サ sa   シ shi  ス su   セ se   ソ so   シャ sha  シュ shu  ショ sho
T  タ ta   チ chi  ツ tsu  テ te   ト to   チャ cha  チュ chu  チョ cho
N  ナ na   ニ ni   ヌ nu   ネ ne   ノ no   ニャ nya  ニュ nyu  ニョ nyo
H  ハ ha   ヒ hi   フ fu   ヘ he   ホ ho   ヒャ hya  ヒュ hyu  ヒョ hyo
M  マ ma   ミ mi   ム mu   メ me   モ mo   ミャ mya  ミュ myu  ミョ myo
Y  ヤ ya          ユ yu          ヨ yo
R  ラ ra   リ ri   ル ru   レ re   ロ ro   リャ rya  リュ ryu  リョ ryo
W  ワ wa
   ン n
G  ガ ga   ギ gi   グ gu   ゲ ge   ゴ go   ギャ gya  ギュ guy  gyo
Z  ザ za   ジ ji   ズ zu   ゼ ze   ゾ zo   ジャ ja   ジュ ju   ジョ jo
D  ダ da   ヂ (ji) ヅ (zu) デ de   ド do
B  バ ba   ビ bi   ブ bu   ベ be   ボ bo   ビャ bya  ビュ byu  byo
P  パ pa   ピ pi   プ pu   ペ pe   ポ po   ピャ pya  ピュ pyu  pyo
F  ファ fa  フィ fi         フェ fe  フォ fo
T  ツァ tsa ティ ti  トゥ tu
W                       ウェ we  ウォ wo

Fullwidth:

   ア a    イ i    ウ u    エ e    オ o
K  カ ka   キ ki   ク ku   ケ ke   コ ko   キャ kya  キュ kyu  キョ kyo
S  サ sa   シ shi  ス su   セ se   ソ so   シャ sha  シュ shu  ショ sho
T  タ ta   チ chi  ツ tsu  テ te   ト to   チャ cha  チュ chu  チョ cho
N  ナ na   ニ ni   ヌ nu   ネ ne   ノ no   ニャ nya  ニュ nyu  ニョ nyo
H  ハ ha   ヒ hi   フ fu   ヘ he   ホ ho   ヒャ hya  ヒュ hyu  ヒョ hyo
M  マ ma   ミ mi   ム mu   メ me   モ mo   ミャ mya  ミュ myu  ミョ myo
Y  ヤ ya          ユ yu          ヨ yo
R  ラ ra   リ ri   ル ru   レ re   ロ ro   リャ rya  リュ ryu  リョ ryo
W  ワ wa
   ン n
G  ガ ga   ギ gi   グ gu   ゲ ge   ゴ go   ギャ gya  ギュ guy  ギョ gyo
Z  ザ za   ジ ji   ズ zu   ゼ ze   ゾ zo   ジャ ja   ジュ ju   ジョ jo
D  ダ da   ヂ (ji) ヅ (zu) デ de   ド do
B  バ ba   ビ bi   ブ bu   ベ be   ボ bo   ビャ bya  ビュ byu  ビョ byo
P  パ pa   ピ pi   プ pu   ペ pe   ポ po   ピャ pya  ピュ pyu  ピョ pyo
F  ファ fa  フィ fi         フェ fe  フォ fo
T  ツァ tsa ティ ti  トゥ tu
W                       ウェ we  ウォ wo

Implementation

The library uses a character mapping based on the code chart of the Halfwidth and Fullwidth Forms Unicode block (U+FF00–FFEF) of The Unicode Standard, Version 13.0.

When converting a String from fullwidth into halfwidth, it takes into account characters using variation sequences (represented as multiple Char in .NET), by removing the variation.

Please note that the library uses Unicode normalization to be able to convert to and from the two representations. Refer to the specific methods for more details.

Alternatives (Windows only)

There are 2 alternatives for .NET using the Windows API (Win32).

  1. P/Invoke to LCMapString and use the LCMAP_FULLWIDTH and LCMAP_HALFWIDTH map flags.
  2. Use Visual Basic's StrConv() function with VbStrConv.Wide or VbStrConv.Narrow conversion arguments, which uses the above P/Invoke method internally, and throws a PlatformNotSupportedException when not on Windows.

Contributing

The source code was developed with the .NET Core 3.1 SDK.

The .sln at the root of the project allows to:

  • Build the project with dotnet build.
  • Run unit tests with dotnet test.
  • Create the NuGet package with dotnet pack.

If you find a problem, please feel free to open an issue or submit a PR. Thanks! 🙏

About

A .NET Standard library to convert between halfwidth and fullwidth Unicode forms.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages