1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! # Chinese Telegraph Code
//!
//! This crate provides utilities for converting Unicode Chinese characters to Chinese telegraph codes.
//!
//! Chinese telegraph codes are numerical codes used historically for transmitting Chinese text over telegraph systems.
//! This crate supports both Traditional Chinese (Taiwan) and Simplified Chinese character sets.
//!
//! ## Features
//!
//! - Convert Chinese characters to telegraph codes
//! - Support for both Traditional and Simplified Chinese
//! - `no_std` compatible (with optional `std` feature for string formatting)
//! - Fast lookups using perfect hash functions
//!
//! ## Usage
//!
//! ```rust
//! use chinese_telegraph::{to_telegraph, to_telegraph_string, Table};
//!
//! // Look up a Traditional Chinese character
//! let code = to_telegraph("這", Table::TW);
//! assert_eq!(code, Some(6638));
//!
//! // Look up a Simplified Chinese character
//! let code = to_telegraph("这", Table::CN);
//! assert_eq!(code, Some(6638));
//!
//! // Search both tables
//! let code = to_telegraph("一", Table::Both);
//! assert_eq!(code, Some(1));
//!
//! // Format as a 4-digit string (requires std feature)
//! # #[cfg(feature = "std")]
//! let formatted = to_telegraph_string("一", Table::Both);
//! # #[cfg(feature = "std")]
//! assert_eq!(formatted, Some("0001".to_string()));
//! ```
/// Simplified Chinese character lookup table.
/// Traditional Chinese character lookup table.
/// Specifies which character table(s) to use for telegraph code lookup.
/// Converts a Chinese character to its telegraph code.
///
/// # Arguments
///
/// * `character` - A string slice containing exactly one Chinese character
/// * `table` - Which character table(s) to search
///
/// # Returns
///
/// Returns `Some(code)` if the character is found in the specified table(s),
/// or `None` if the character is not found or if the input contains more than one character.
///
/// # Examples
///
/// ```rust
/// use chinese_telegraph::{to_telegraph, Table};
///
/// // Traditional Chinese character
/// assert_eq!(to_telegraph("這", Table::TW), Some(6638));
///
/// // Simplified Chinese character
/// assert_eq!(to_telegraph("这", Table::CN), Some(6638));
///
/// // Character found in both tables
/// assert_eq!(to_telegraph("一", Table::Both), Some(1));
///
/// // Unknown character
/// assert_eq!(to_telegraph("🦀", Table::Both), None);
///
/// // Multiple characters
/// assert_eq!(to_telegraph("這是", Table::Both), None);
/// ```
extern crate std;
/// Converts a Chinese character to its telegraph code formatted as a 4-digit string.
///
/// This function is only available when the `std` feature is enabled.
///
/// # Arguments
///
/// * `character` - A string slice containing exactly one Chinese character
/// * `table` - Which character table(s) to search
///
/// # Returns
///
/// Returns `Some(formatted_code)` if the character is found, where the code is
/// formatted as a 4-digit string with leading zeros, or `None` if the character
/// is not found or if the input contains more than one character.
///
/// # Examples
///
/// ```rust
/// use chinese_telegraph::{to_telegraph_string, Table};
///
/// assert_eq!(to_telegraph_string("一", Table::Both), Some("0001".to_string()));
/// assert_eq!(to_telegraph_string("這", Table::TW), Some("6638".to_string()));
/// assert_eq!(to_telegraph_string("🦀", Table::Both), None);
/// ```