character-encoding-utils 0.0.11__tar.gz → 0.0.13__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 TakWolf
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,29 +1,30 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: character-encoding-utils
3
- Version: 0.0.11
3
+ Version: 0.0.13
4
4
  Summary: Some character encoding utils
5
5
  Keywords: encoding,gb2312,big5,shift-jis,ksx1001
6
6
  Author: TakWolf
7
7
  License-Expression: MIT
8
+ License-File: LICENSE
8
9
  Classifier: Programming Language :: Python :: 3
9
10
  Classifier: Programming Language :: Python :: 3.10
10
11
  Classifier: Programming Language :: Python :: 3.11
11
12
  Classifier: Programming Language :: Python :: 3.12
12
13
  Classifier: Programming Language :: Python :: 3.13
13
14
  Classifier: Programming Language :: Python :: 3.14
14
- Classifier: License :: OSI Approved :: MIT License
15
+ Classifier: Programming Language :: Python :: 3.15
15
16
  Classifier: Operating System :: OS Independent
16
17
  Maintainer: TakWolf
17
18
  Requires-Python: >=3.10
18
19
  Project-URL: Homepage, https://github.com/TakWolf/character-encoding-utils
19
- Project-URL: Issues, https://github.com/TakWolf/character-encoding-utils/issues
20
20
  Project-URL: Source, https://github.com/TakWolf/character-encoding-utils
21
+ Project-URL: Issues, https://github.com/TakWolf/character-encoding-utils/issues
21
22
  Description-Content-Type: text/markdown
22
23
 
23
24
  # Character Encoding Utils
24
25
 
25
- [![Python](https://img.shields.io/badge/python-3.10-brightgreen)](https://www.python.org)
26
- [![PyPI](https://img.shields.io/pypi/v/character-encoding-utils)](https://pypi.org/project/character-encoding-utils/)
26
+ [![Python](https://img.shields.io/badge/python-3.10-brightgreen?style=flat-square)](https://www.python.org)
27
+ [![PyPI](https://img.shields.io/pypi/v/character-encoding-utils?style=flat-square)](https://pypi.org/project/character-encoding-utils/)
27
28
 
28
29
  Some [character encoding](https://en.wikipedia.org/wiki/Character_encoding) utils.
29
30
 
@@ -1,7 +1,7 @@
1
1
  # Character Encoding Utils
2
2
 
3
- [![Python](https://img.shields.io/badge/python-3.10-brightgreen)](https://www.python.org)
4
- [![PyPI](https://img.shields.io/pypi/v/character-encoding-utils)](https://pypi.org/project/character-encoding-utils/)
3
+ [![Python](https://img.shields.io/badge/python-3.10-brightgreen?style=flat-square)](https://www.python.org)
4
+ [![PyPI](https://img.shields.io/pypi/v/character-encoding-utils?style=flat-square)](https://pypi.org/project/character-encoding-utils/)
5
5
 
6
6
  Some [character encoding](https://en.wikipedia.org/wiki/Character_encoding) utils.
7
7
 
@@ -0,0 +1,46 @@
1
+ [project]
2
+ name = "character-encoding-utils"
3
+ version = "0.0.13"
4
+ description = "Some character encoding utils"
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ keywords = [
9
+ "encoding",
10
+ "gb2312",
11
+ "big5",
12
+ "shift-jis",
13
+ "ksx1001",
14
+ ]
15
+ requires-python = ">=3.10"
16
+ classifiers = [
17
+ "Programming Language :: Python :: 3",
18
+ "Programming Language :: Python :: 3.10",
19
+ "Programming Language :: Python :: 3.11",
20
+ "Programming Language :: Python :: 3.12",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Programming Language :: Python :: 3.14",
23
+ "Programming Language :: Python :: 3.15",
24
+ "Operating System :: OS Independent",
25
+ ]
26
+
27
+ [[project.authors]]
28
+ name = "TakWolf"
29
+
30
+ [[project.maintainers]]
31
+ name = "TakWolf"
32
+
33
+ [project.urls]
34
+ Homepage = "https://github.com/TakWolf/character-encoding-utils"
35
+ Source = "https://github.com/TakWolf/character-encoding-utils"
36
+ Issues = "https://github.com/TakWolf/character-encoding-utils/issues"
37
+
38
+ [dependency-groups]
39
+ dev = ["pytest>=9.1.1"]
40
+
41
+ [build-system]
42
+ requires = ["uv_build"]
43
+ build-backend = "uv_build"
44
+
45
+ [tool.pytest.ini_options]
46
+ addopts = ["--import-mode=importlib"]
@@ -1,13 +1,10 @@
1
- [build-system]
2
- requires = ["uv_build"]
3
- build-backend = "uv_build"
4
-
5
1
  [project]
6
2
  name = "character-encoding-utils"
7
- version = "0.0.11"
3
+ version = "0.0.13"
8
4
  description = "Some character encoding utils"
9
5
  readme = "README.md"
10
6
  license = "MIT"
7
+ license-files = ["LICENSE"]
11
8
  authors = [
12
9
  { name = "TakWolf" },
13
10
  ]
@@ -23,16 +20,25 @@ classifiers = [
23
20
  "Programming Language :: Python :: 3.12",
24
21
  "Programming Language :: Python :: 3.13",
25
22
  "Programming Language :: Python :: 3.14",
26
- "License :: OSI Approved :: MIT License",
23
+ "Programming Language :: Python :: 3.15",
27
24
  "Operating System :: OS Independent",
28
25
  ]
29
26
 
30
27
  [dependency-groups]
31
28
  dev = [
32
- "pytest>=8.4.2",
29
+ "pytest>=9.1.1",
33
30
  ]
34
31
 
35
32
  [project.urls]
36
33
  Homepage = "https://github.com/TakWolf/character-encoding-utils"
37
34
  Source = "https://github.com/TakWolf/character-encoding-utils"
38
35
  Issues = "https://github.com/TakWolf/character-encoding-utils/issues"
36
+
37
+ [build-system]
38
+ requires = ["uv_build"]
39
+ build-backend = "uv_build"
40
+
41
+ [tool.pytest.ini_options]
42
+ addopts = [
43
+ "--import-mode=importlib",
44
+ ]
@@ -8,7 +8,7 @@ class Big5EncodeError(Big5Exception):
8
8
  position: int
9
9
  reason: str
10
10
 
11
- def __init__(self, obj: str, position: int, reason: str):
11
+ def __init__(self, obj: str, position: int, reason: str) -> None:
12
12
  self.obj = obj
13
13
  self.position = position
14
14
  self.reason = reason
@@ -22,7 +22,7 @@ class Big5DecodeError(Big5Exception):
22
22
  position: int
23
23
  reason: str
24
24
 
25
- def __init__(self, obj: bytes, position: int, reason: str):
25
+ def __init__(self, obj: bytes, position: int, reason: str) -> None:
26
26
  self.obj = obj
27
27
  self.position = position
28
28
  self.reason = reason
@@ -37,32 +37,33 @@ class Big5DecodeError(Big5Exception):
37
37
  def encode(cs: str) -> bytes:
38
38
  bs = bytearray()
39
39
  for position, c in enumerate(cs):
40
- if c == '〸': # 0x3038
41
- bs.extend(b'\xa2\xcc')
42
- elif c == '〹': # 0x3039
43
- bs.extend(b'\xa2\xcd')
44
- elif c == '〺': # 0x303A
45
- bs.extend(b'\xa2\xce')
46
- elif c == '十': # 0x5341
47
- bs.extend(b'\xa4\x51')
48
- elif c == '卄': # 0x5344
49
- # Big5 不包含汉字的 '卄'
50
- raise Big5EncodeError(c, position, 'illegal multibyte sequence')
51
- elif c == '卅': # 0x5345
52
- bs.extend(b'\xa4\xca')
53
- elif c == '/': # 0xFF0F
54
- bs.extend(b'\xa1\xfe')
55
- elif c == '\': # 0xFF3C
56
- bs.extend(b'\xa2\x40')
57
- elif c == '∕': # 0x2215
58
- bs.extend(b'\xa2\x41')
59
- elif c == '﹨': # 0xFE68
60
- bs.extend(b'\xa2\x42')
61
- else:
62
- try:
63
- bs.extend(c.encode('big5'))
64
- except UnicodeEncodeError as e:
65
- raise Big5EncodeError(c, position, e.reason) from e
40
+ match c:
41
+ case '〸': # 0x3038
42
+ bs.extend(b'\xa2\xcc')
43
+ case '〹': # 0x3039
44
+ bs.extend(b'\xa2\xcd')
45
+ case '〺': # 0x303A
46
+ bs.extend(b'\xa2\xce')
47
+ case '十': # 0x5341
48
+ bs.extend(b'\xa4\x51')
49
+ case '卄': # 0x5344
50
+ # Big5 不包含汉字的 '卄'
51
+ raise Big5EncodeError(c, position, 'illegal multibyte sequence')
52
+ case '卅': # 0x5345
53
+ bs.extend(b'\xa4\xca')
54
+ case '/': # 0xFF0F
55
+ bs.extend(b'\xa1\xfe')
56
+ case '\': # 0xFF3C
57
+ bs.extend(b'\xa2\x40')
58
+ case '∕': # 0x2215
59
+ bs.extend(b'\xa2\x41')
60
+ case '﹨': # 0xFE68
61
+ bs.extend(b'\xa2\x42')
62
+ case _:
63
+ try:
64
+ bs.extend(c.encode('big5'))
65
+ except UnicodeEncodeError as e:
66
+ raise Big5EncodeError(c, position, e.reason) from e
66
67
  return bytes(bs)
67
68
 
68
69
 
@@ -81,36 +82,37 @@ def decode(bs: bytes | bytearray) -> str:
81
82
  bc.append(bs[cursor])
82
83
  cursor += 1
83
84
 
84
- if bc == b'\xa2\xcc':
85
- cs.append('〸') # 0x3038
86
- elif bc == b'\xa2\xcd':
87
- cs.append('〹') # 0x3039
88
- elif bc == b'\xa2\xce':
89
- cs.append('〺') # 0x303A
90
- elif bc == b'\xa4\x51':
91
- cs.append('十') # 0x5341
92
- elif bc == b'\xa4\xca':
93
- cs.append('卅') # 0x5345
94
- elif bc == b'\xa1\xfe':
95
- cs.append('/') # 0xFF0F
96
- elif bc == b'\xa2\x40':
97
- cs.append('\') # 0xFF3C
98
- elif bc == b'\xa2\x41':
99
- cs.append('∕') # 0x2215
100
- elif bc == b'\xa2\x42':
101
- cs.append('﹨') # 0xFE68
102
- else:
103
- try:
104
- cs.append(bc.decode('big5'))
105
- except UnicodeDecodeError as e:
106
- raise Big5DecodeError(bc, passed, e.reason) from e
85
+ match bc:
86
+ case b'\xa2\xcc':
87
+ cs.append('〸') # 0x3038
88
+ case b'\xa2\xcd':
89
+ cs.append('〹') # 0x3039
90
+ case b'\xa2\xce':
91
+ cs.append('〺') # 0x303A
92
+ case b'\xa4\x51':
93
+ cs.append('十') # 0x5341
94
+ case b'\xa4\xca':
95
+ cs.append('卅') # 0x5345
96
+ case b'\xa1\xfe':
97
+ cs.append('/') # 0xFF0F
98
+ case b'\xa2\x40':
99
+ cs.append('\') # 0xFF3C
100
+ case b'\xa2\x41':
101
+ cs.append('∕') # 0x2215
102
+ case b'\xa2\x42':
103
+ cs.append('﹨') # 0xFE68
104
+ case _:
105
+ try:
106
+ cs.append(bc.decode('big5'))
107
+ except UnicodeDecodeError as e:
108
+ raise Big5DecodeError(bc, passed, e.reason) from e
107
109
  passed += len(bc)
108
110
  return ''.join(cs)
109
111
 
110
112
 
111
113
  def query_code(c: str) -> int:
112
114
  if len(c) != 1:
113
- raise Big5Exception('Must be one character')
115
+ raise Big5Exception('must be one character')
114
116
  try:
115
117
  bs = encode(c)
116
118
  except Big5EncodeError as e:
@@ -158,39 +160,39 @@ def _build_alphabet_by_codes_between(code_start: int, code_end: int) -> list[str
158
160
  return alphabet
159
161
 
160
162
 
161
- _alphabet_other = _build_alphabet_by_codes_between(0xA140, 0xA3BF)
162
- _alphabet_level_1 = _build_alphabet_by_codes_between(0xA440, 0xC67E)
163
- _alphabet_level_2 = _build_alphabet_by_codes_between(0xC940, 0xF9D5)
164
- _alphabet = _alphabet_other + _alphabet_level_1 + _alphabet_level_2
163
+ _ALPHABET_OTHER = _build_alphabet_by_codes_between(0xA140, 0xA3BF)
164
+ _ALPHABET_LEVEL_1 = _build_alphabet_by_codes_between(0xA440, 0xC67E)
165
+ _ALPHABET_LEVEL_2 = _build_alphabet_by_codes_between(0xC940, 0xF9D5)
166
+ _ALPHABET = _ALPHABET_OTHER + _ALPHABET_LEVEL_1 + _ALPHABET_LEVEL_2
165
167
 
166
168
 
167
169
  def get_alphabet_other() -> list[str]:
168
- return list(_alphabet_other)
170
+ return _ALPHABET_OTHER.copy()
169
171
 
170
172
 
171
173
  def get_alphabet_level_1() -> list[str]:
172
- return list(_alphabet_level_1)
174
+ return _ALPHABET_LEVEL_1.copy()
173
175
 
174
176
 
175
177
  def get_alphabet_level_2() -> list[str]:
176
- return list(_alphabet_level_2)
178
+ return _ALPHABET_LEVEL_2.copy()
177
179
 
178
180
 
179
181
  def get_alphabet() -> list[str]:
180
- return list(_alphabet)
182
+ return _ALPHABET.copy()
181
183
 
182
184
 
183
185
  def get_other_count() -> int:
184
- return len(_alphabet_other)
186
+ return len(_ALPHABET_OTHER)
185
187
 
186
188
 
187
189
  def get_level_1_count() -> int:
188
- return len(_alphabet_level_1)
190
+ return len(_ALPHABET_LEVEL_1)
189
191
 
190
192
 
191
193
  def get_level_2_count() -> int:
192
- return len(_alphabet_level_2)
194
+ return len(_ALPHABET_LEVEL_2)
193
195
 
194
196
 
195
197
  def get_count() -> int:
196
- return len(_alphabet)
198
+ return len(_ALPHABET)
@@ -11,7 +11,7 @@ class GB2312EncodeError(GB2312Exception):
11
11
  position: int
12
12
  reason: str
13
13
 
14
- def __init__(self, obj: str, position: int, reason: str):
14
+ def __init__(self, obj: str, position: int, reason: str) -> None:
15
15
  self.obj = obj
16
16
  self.position = position
17
17
  self.reason = reason
@@ -25,7 +25,7 @@ class GB2312DecodeError(GB2312Exception):
25
25
  position: int
26
26
  reason: str
27
27
 
28
- def __init__(self, obj: bytes, position: int, reason: str):
28
+ def __init__(self, obj: bytes, position: int, reason: str) -> None:
29
29
  self.obj = obj
30
30
  self.position = position
31
31
  self.reason = reason
@@ -72,7 +72,7 @@ def decode(bs: bytes | bytearray) -> str:
72
72
 
73
73
  def query_coord(c: str) -> tuple[int, int]:
74
74
  if len(c) != 1:
75
- raise GB2312Exception('Must be one character')
75
+ raise GB2312Exception('must be one character')
76
76
  try:
77
77
  bs = encode(c)
78
78
  except GB2312EncodeError as e:
@@ -124,39 +124,39 @@ def _build_alphabet_by_rows_between(row_start: int, row_end: int) -> list[str]:
124
124
  return alphabet
125
125
 
126
126
 
127
- _alphabet_other = _build_alphabet_by_rows_between(1, 9)
128
- _alphabet_level_1 = _build_alphabet_by_rows_between(16, 55)
129
- _alphabet_level_2 = _build_alphabet_by_rows_between(56, 87)
130
- _alphabet = _alphabet_other + _alphabet_level_1 + _alphabet_level_2
127
+ _ALPHABET_OTHER = _build_alphabet_by_rows_between(1, 9)
128
+ _ALPHABET_LEVEL_1 = _build_alphabet_by_rows_between(16, 55)
129
+ _ALPHABET_LEVEL_2 = _build_alphabet_by_rows_between(56, 87)
130
+ _ALPHABET = _ALPHABET_OTHER + _ALPHABET_LEVEL_1 + _ALPHABET_LEVEL_2
131
131
 
132
132
 
133
133
  def get_alphabet_other() -> list[str]:
134
- return list(_alphabet_other)
134
+ return _ALPHABET_OTHER.copy()
135
135
 
136
136
 
137
137
  def get_alphabet_level_1() -> list[str]:
138
- return list(_alphabet_level_1)
138
+ return _ALPHABET_LEVEL_1.copy()
139
139
 
140
140
 
141
141
  def get_alphabet_level_2() -> list[str]:
142
- return list(_alphabet_level_2)
142
+ return _ALPHABET_LEVEL_2.copy()
143
143
 
144
144
 
145
145
  def get_alphabet() -> list[str]:
146
- return list(_alphabet)
146
+ return _ALPHABET.copy()
147
147
 
148
148
 
149
149
  def get_other_count() -> int:
150
- return len(_alphabet_other)
150
+ return len(_ALPHABET_OTHER)
151
151
 
152
152
 
153
153
  def get_level_1_count() -> int:
154
- return len(_alphabet_level_1)
154
+ return len(_ALPHABET_LEVEL_1)
155
155
 
156
156
 
157
157
  def get_level_2_count() -> int:
158
- return len(_alphabet_level_2)
158
+ return len(_ALPHABET_LEVEL_2)
159
159
 
160
160
 
161
161
  def get_count() -> int:
162
- return len(_alphabet)
162
+ return len(_ALPHABET)
@@ -11,7 +11,7 @@ class KSX1001EncodeError(KSX1001Exception):
11
11
  position: int
12
12
  reason: str
13
13
 
14
- def __init__(self, obj: str, position: int, reason: str):
14
+ def __init__(self, obj: str, position: int, reason: str) -> None:
15
15
  self.obj = obj
16
16
  self.position = position
17
17
  self.reason = reason
@@ -25,7 +25,7 @@ class KSX1001DecodeError(KSX1001Exception):
25
25
  position: int
26
26
  reason: str
27
27
 
28
- def __init__(self, obj: bytes, position: int, reason: str):
28
+ def __init__(self, obj: bytes, position: int, reason: str) -> None:
29
29
  self.obj = obj
30
30
  self.position = position
31
31
  self.reason = reason
@@ -41,7 +41,7 @@ def encode(cs: str) -> bytes:
41
41
  bs = bytearray()
42
42
  for position, c in enumerate(cs):
43
43
  try:
44
- bs.extend(c.encode('ksx1001'))
44
+ bs.extend(c.encode('euc_kr'))
45
45
  except UnicodeEncodeError as e:
46
46
  raise KSX1001EncodeError(c, position, e.reason) from e
47
47
  return bytes(bs)
@@ -77,7 +77,7 @@ def decode(bs: bytes | bytearray) -> str:
77
77
  cs.append(chr(0x3164)) # Hangul Filler
78
78
  else:
79
79
  try:
80
- cs.append(bc.decode('ksx1001'))
80
+ cs.append(bc.decode('euc_kr'))
81
81
  except UnicodeDecodeError as e:
82
82
  raise KSX1001DecodeError(bc, passed, e.reason) from e
83
83
  passed += len(bc)
@@ -86,7 +86,7 @@ def decode(bs: bytes | bytearray) -> str:
86
86
 
87
87
  def query_coord(c: str) -> tuple[int, int]:
88
88
  if len(c) != 1:
89
- raise KSX1001Exception('Must be one character')
89
+ raise KSX1001Exception('must be one character')
90
90
  try:
91
91
  bs = encode(c)
92
92
  except KSX1001EncodeError as e:
@@ -100,7 +100,7 @@ def query_coord(c: str) -> tuple[int, int]:
100
100
  return row, col
101
101
 
102
102
 
103
- def query_chr(row: int, col: int):
103
+ def query_chr(row: int, col: int) -> str:
104
104
  if row < 1 or row > 94 or col < 1 or col > 94:
105
105
  raise KSX1001Exception(f"'row' and 'col' must between 1 and 94")
106
106
  try:
@@ -140,39 +140,39 @@ def _build_alphabet_by_rows_between(row_start: int, row_end: int) -> list[str]:
140
140
  return alphabet
141
141
 
142
142
 
143
- _alphabet_other = _build_alphabet_by_rows_between(1, 12)
144
- _alphabet_syllable = _build_alphabet_by_rows_between(16, 40)
145
- _alphabet_hanja = _build_alphabet_by_rows_between(42, 93)
146
- _alphabet = _alphabet_other + _alphabet_syllable + _alphabet_hanja
143
+ _ALPHABET_OTHER = _build_alphabet_by_rows_between(1, 12)
144
+ _ALPHABET_SYLLABLE = _build_alphabet_by_rows_between(16, 40)
145
+ _ALPHABET_HANJA = _build_alphabet_by_rows_between(42, 93)
146
+ _ALPHABET = _ALPHABET_OTHER + _ALPHABET_SYLLABLE + _ALPHABET_HANJA
147
147
 
148
148
 
149
149
  def get_alphabet_other() -> list[str]:
150
- return list(_alphabet_other)
150
+ return _ALPHABET_OTHER.copy()
151
151
 
152
152
 
153
153
  def get_alphabet_syllable() -> list[str]:
154
- return list(_alphabet_syllable)
154
+ return _ALPHABET_SYLLABLE.copy()
155
155
 
156
156
 
157
157
  def get_alphabet_hanja() -> list[str]:
158
- return list(_alphabet_hanja)
158
+ return _ALPHABET_HANJA.copy()
159
159
 
160
160
 
161
161
  def get_alphabet() -> list[str]:
162
- return list(_alphabet)
162
+ return _ALPHABET.copy()
163
163
 
164
164
 
165
165
  def get_other_count() -> int:
166
- return len(_alphabet_other)
166
+ return len(_ALPHABET_OTHER)
167
167
 
168
168
 
169
169
  def get_syllable_count() -> int:
170
- return len(_alphabet_syllable)
170
+ return len(_ALPHABET_SYLLABLE)
171
171
 
172
172
 
173
173
  def get_hanja_count() -> int:
174
- return len(_alphabet_hanja)
174
+ return len(_ALPHABET_HANJA)
175
175
 
176
176
 
177
177
  def get_count() -> int:
178
- return len(_alphabet)
178
+ return len(_ALPHABET)
@@ -10,7 +10,7 @@ class ShiftJISEncodeError(ShiftJISException):
10
10
  position: int
11
11
  reason: str
12
12
 
13
- def __init__(self, obj: str, position: int, reason: str):
13
+ def __init__(self, obj: str, position: int, reason: str) -> None:
14
14
  self.obj = obj
15
15
  self.position = position
16
16
  self.reason = reason
@@ -24,7 +24,7 @@ class ShiftJISDecodeError(ShiftJISException):
24
24
  position: int
25
25
  reason: str
26
26
 
27
- def __init__(self, obj: bytes, position: int, reason: str):
27
+ def __init__(self, obj: bytes, position: int, reason: str) -> None:
28
28
  self.obj = obj
29
29
  self.position = position
30
30
  self.reason = reason
@@ -39,15 +39,16 @@ class ShiftJISDecodeError(ShiftJISException):
39
39
  def encode(cs: str) -> bytes:
40
40
  bs = bytearray()
41
41
  for position, c in enumerate(cs):
42
- if c == '\\':
43
- raise ShiftJISEncodeError(c, position, f"in 'shift-jis' the character '\\' is replaced with '¥'")
44
- elif c == '~':
45
- raise ShiftJISEncodeError(c, position, f"in 'shift-jis' the character '~' is replaced with '‾'")
46
- else:
47
- try:
48
- bs.extend(c.encode('shift-jis'))
49
- except UnicodeEncodeError as e:
50
- raise ShiftJISEncodeError(c, position, e.reason) from e
42
+ match c:
43
+ case '\\':
44
+ raise ShiftJISEncodeError(c, position, f"in 'shift-jis' the character '\\' is replaced with '¥'")
45
+ case '~':
46
+ raise ShiftJISEncodeError(c, position, f"in 'shift-jis' the character '~' is replaced with '‾'")
47
+ case _:
48
+ try:
49
+ bs.extend(c.encode('shift-jis'))
50
+ except UnicodeEncodeError as e:
51
+ raise ShiftJISEncodeError(c, position, e.reason) from e
51
52
  return bytes(bs)
52
53
 
53
54
 
@@ -66,15 +67,16 @@ def decode(bs: bytes | bytearray) -> str:
66
67
  bc.append(bs[cursor])
67
68
  cursor += 1
68
69
 
69
- if bc == b'\\':
70
- cs.append('¥')
71
- elif bc == b'~':
72
- cs.append('‾')
73
- else:
74
- try:
75
- cs.append(bc.decode('shift-jis'))
76
- except UnicodeDecodeError as e:
77
- raise ShiftJISDecodeError(bc, passed, e.reason) from e
70
+ match bc:
71
+ case b'\\':
72
+ cs.append('¥')
73
+ case b'~':
74
+ cs.append('‾')
75
+ case _:
76
+ try:
77
+ cs.append(bc.decode('shift-jis'))
78
+ except UnicodeDecodeError as e:
79
+ raise ShiftJISDecodeError(bc, passed, e.reason) from e
78
80
  passed += len(bc)
79
81
  return ''.join(cs)
80
82
 
@@ -170,58 +172,58 @@ def _build_alphabet_double_byte_kanji() -> list[str]:
170
172
  return alphabet
171
173
 
172
174
 
173
- _alphabet_single_byte_ascii_control = _build_alphabet_single_byte(0x00, 0x1F)
174
- _alphabet_single_byte_ascii_control.append(chr(0x7F))
175
- _alphabet_single_byte_ascii_printable = _build_alphabet_single_byte(0x20, 0x7E)
176
- _alphabet_single_byte_half_width_katakana = _build_alphabet_single_byte(0xA1, 0xDF)
177
- _alphabet_double_byte_other = _build_alphabet_double_byte_other()
178
- _alphabet_double_byte_kanji = _build_alphabet_double_byte_kanji()
179
- _alphabet = _alphabet_single_byte_ascii_control + _alphabet_single_byte_ascii_printable + _alphabet_single_byte_half_width_katakana + _alphabet_double_byte_other + _alphabet_double_byte_kanji
175
+ _ALPHABET_SINGLE_BYTE_ASCII_CONTROL = _build_alphabet_single_byte(0x00, 0x1F)
176
+ _ALPHABET_SINGLE_BYTE_ASCII_CONTROL.append(chr(0x7F))
177
+ _ALPHABET_SINGLE_BYTE_ASCII_PRINTABLE = _build_alphabet_single_byte(0x20, 0x7E)
178
+ _ALPHABET_SINGLE_BYTE_HALF_WIDTH_KATAKANA = _build_alphabet_single_byte(0xA1, 0xDF)
179
+ _ALPHABET_DOUBLE_BYTE_OTHER = _build_alphabet_double_byte_other()
180
+ _ALPHABET_DOUBLE_BYTE_KANJI = _build_alphabet_double_byte_kanji()
181
+ _ALPHABET = _ALPHABET_SINGLE_BYTE_ASCII_CONTROL + _ALPHABET_SINGLE_BYTE_ASCII_PRINTABLE + _ALPHABET_SINGLE_BYTE_HALF_WIDTH_KATAKANA + _ALPHABET_DOUBLE_BYTE_OTHER + _ALPHABET_DOUBLE_BYTE_KANJI
180
182
 
181
183
 
182
184
  def get_alphabet_single_byte_ascii_control() -> list[str]:
183
- return list(_alphabet_single_byte_ascii_control)
185
+ return _ALPHABET_SINGLE_BYTE_ASCII_CONTROL.copy()
184
186
 
185
187
 
186
188
  def get_alphabet_single_byte_ascii_printable() -> list[str]:
187
- return list(_alphabet_single_byte_ascii_printable)
189
+ return _ALPHABET_SINGLE_BYTE_ASCII_PRINTABLE.copy()
188
190
 
189
191
 
190
192
  def get_alphabet_single_byte_half_width_katakana() -> list[str]:
191
- return list(_alphabet_single_byte_half_width_katakana)
193
+ return _ALPHABET_SINGLE_BYTE_HALF_WIDTH_KATAKANA.copy()
192
194
 
193
195
 
194
196
  def get_alphabet_double_byte_other() -> list[str]:
195
- return list(_alphabet_double_byte_other)
197
+ return _ALPHABET_DOUBLE_BYTE_OTHER.copy()
196
198
 
197
199
 
198
200
  def get_alphabet_double_byte_kanji() -> list[str]:
199
- return list(_alphabet_double_byte_kanji)
201
+ return _ALPHABET_DOUBLE_BYTE_KANJI.copy()
200
202
 
201
203
 
202
204
  def get_alphabet() -> list[str]:
203
- return list(_alphabet)
205
+ return _ALPHABET.copy()
204
206
 
205
207
 
206
208
  def get_single_byte_ascii_control_count() -> int:
207
- return len(_alphabet_single_byte_ascii_control)
209
+ return len(_ALPHABET_SINGLE_BYTE_ASCII_CONTROL)
208
210
 
209
211
 
210
212
  def get_single_byte_ascii_printable_count() -> int:
211
- return len(_alphabet_single_byte_ascii_printable)
213
+ return len(_ALPHABET_SINGLE_BYTE_ASCII_PRINTABLE)
212
214
 
213
215
 
214
216
  def get_single_byte_half_width_katakana_count() -> int:
215
- return len(_alphabet_single_byte_half_width_katakana)
217
+ return len(_ALPHABET_SINGLE_BYTE_HALF_WIDTH_KATAKANA)
216
218
 
217
219
 
218
220
  def get_double_byte_other_count() -> int:
219
- return len(_alphabet_double_byte_other)
221
+ return len(_ALPHABET_DOUBLE_BYTE_OTHER)
220
222
 
221
223
 
222
224
  def get_double_byte_kanji_count() -> int:
223
- return len(_alphabet_double_byte_kanji)
225
+ return len(_ALPHABET_DOUBLE_BYTE_KANJI)
224
226
 
225
227
 
226
228
  def get_count() -> int:
227
- return len(_alphabet)
229
+ return len(_ALPHABET)