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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//! # `ChrW` Function
//!
//! Returns a `String` containing the Unicode character associated with the specified character code.
//! The "W" suffix indicates this is the wide (Unicode) version of the `Chr` function.
//!
//! ## Syntax
//!
//! ```vb
//! ChrW(charcode)
//! ```
//!
//! ## Parameters
//!
//! - **charcode**: Required. `Long`. A numeric expression that identifies a Unicode character.
//! Valid values are -32768 to 65535. However, values -32768 to -1 are treated as 65536 + value.
//!
//! ## Returns
//!
//! Returns a `String` containing a single Unicode character corresponding to the specified code.
//!
//! ## Remarks
//!
//! - `ChrW` is used to return Unicode characters (wide characters).
//! - The W suffix stands for "Wide", distinguishing it from the ANSI `ChrB` function.
//! - Valid Unicode code points range from 0 to 65535 (0x0000 to 0xFFFF) in the Basic Multilingual Plane.
//! - Negative values from -32768 to -1 are converted by adding 65536, allowing access to the full range.
//! - `ChrW` is essential for working with international characters, symbols, and emoji within the BMP.
//! - For ANSI characters (0-255), `ChrW` and `Chr` produce the same results on systems using single-byte character sets.
//! - Characters outside the Basic Multilingual Plane (above 65535) require surrogate pairs in VB6.
//! - If charcode is outside the valid range, a runtime error occurs (Error 5: Invalid procedure call or argument).
//!
//! ## Typical Uses
//!
//! 1. **International text support** - Create strings with characters from various languages
//! 2. **Unicode symbol insertion** - Insert mathematical symbols, currency symbols, arrows, etc.
//! 3. **XML/HTML entity handling** - Convert numeric character references to actual characters
//! 4. **Special character creation** - Generate Unicode control characters and formatting marks
//! 5. **Cross-platform text** - Ensure consistent character representation across different systems
//! 6. **Unicode file processing** - Read and write Unicode text files correctly
//! 7. **Internationalization (i18n)** - Support multiple languages in applications
//!
//! ## Basic Examples
//!
//! ```vb
//! ' Example 1: Simple Unicode character
//! Dim ch As String
//! ch = ChrW(65) ' Returns "A" (same as Chr for ASCII range)
//! ```
//!
//! ```vb
//! ' Example 2: Euro symbol
//! Dim euro As String
//! euro = ChrW(8364) ' Returns "€"
//! ```
//!
//! ```vb
//! ' Example 3: Greek letter
//! Dim alpha As String
//! alpha = ChrW(945) ' Returns "α" (Greek small letter alpha)
//! ```
//!
//! ```vb
//! ' Example 4: Chinese character
//! Dim hanzi As String
//! hanzi = ChrW(20013) ' Returns "中" (Chinese character for "middle")
//! ```
//!
//! ## Common Patterns
//!
//! ### Unicode Line Separator
//! ```vb
//! Const UNICODE_LINE_SEP = 8232
//! text = "Line 1" & ChrW(UNICODE_LINE_SEP) & "Line 2"
//! ```
//!
//! ### Bullet Point List
//! ```vb
//! Dim bullet As String
//! bullet = ChrW(8226) ' "•"
//! list = bullet & " Item 1" & vbCrLf & bullet & " Item 2"
//! ```
//!
//! ### Copyright Symbol
//! ```vb
//! Dim copyright As String
//! copyright = ChrW(169) ' "©"
//! notice = "Copyright " & copyright & " 2025"
//! ```
//!
//! ### Mathematical Symbols
//! ```vb
//! Dim infinity As String, pi As String
//! infinity = ChrW(8734) ' "∞"
//! pi = ChrW(960) ' "π"
//! equation = pi & " " & ChrW(8776) & " 3.14" ' "π ≈ 3.14"
//! ```
//!
//! ### Arrow Symbols
//! ```vb
//! Dim rightArrow As String
//! rightArrow = ChrW(8594) ' "→"
//! leftArrow = ChrW(8592) ' "←"
//! ```
//!
//! ### Non-Breaking Space
//! ```vb
//! Dim nbsp As String
//! nbsp = ChrW(160) ' Non-breaking space
//! text = "Price:" & nbsp & "$100"
//! ```
//!
//! ### Emoji and Symbols (BMP only)
//! ```vb
//! Dim heart As String, star As String
//! heart = ChrW(9829) ' "♥"
//! star = ChrW(9733) ' "★"
//! ```
//!
//! ### Zero-Width Characters
//! ```vb
//! Dim zwj As String
//! zwj = ChrW(8205) ' Zero-width joiner
//! ```
//!
//! ### Currency Symbols
//! ```vb
//! Dim pound As String, yen As String
//! pound = ChrW(163) ' "£"
//! yen = ChrW(165) ' "¥"
//! ```
//!
//! ### Diacritical Marks
//! ```vb
//! Dim acute As String
//! acute = ChrW(180) ' "´" (acute accent)
//! combined = "e" & ChrW(769) ' Combining acute accent
//! ```
//!
//! ## Advanced Examples
//!
//! ### Building Multilingual Text
//! ```vb
//! Function GetGreeting(language As String) As String
//! Select Case language
//! Case "chinese"
//! GetGreeting = ChrW(20320) & ChrW(22909) ' "你好"
//! Case "japanese"
//! GetGreeting = ChrW(12371) & ChrW(12435) & ChrW(12395) & ChrW(12385) & ChrW(12399) ' "こんにちは"
//! Case "korean"
//! GetGreeting = ChrW(50504) & ChrW(45397) & ChrW(54616) & ChrW(49464) & ChrW(50836) ' "안녕하세요"
//! Case "russian"
//! GetGreeting = ChrW(1055) & ChrW(1088) & ChrW(1080) & ChrW(1074) & ChrW(1077) & ChrW(1090) ' "Привет"
//! Case Else
//! GetGreeting = "Hello"
//! End Select
//! End Function
//! ```
//!
//! ### HTML Entity Decoder
//! ```vb
//! Function DecodeNumericEntity(entity As String) As String
//! ' Decode &#nnnn; or &#xhhhh; entities
//! Dim code As Long
//!
//! If InStr(entity, "&#x") > 0 Then
//! ' Hexadecimal entity
//! code = CLng("&H" & Mid(entity, 4, Len(entity) - 4))
//! ElseIf InStr(entity, "&#") > 0 Then
//! ' Decimal entity
//! code = CLng(Mid(entity, 3, Len(entity) - 3))
//! End If
//!
//! If code >= 0 And code <= 65535 Then
//! DecodeNumericEntity = ChrW(code)
//! End If
//! End Function
//! ```
//!
//! ### Unicode Range Validator
//! ```vb
//! Function IsInUnicodeRange(char As String, rangeStart As Long, rangeEnd As Long) As Boolean
//! If Len(char) = 0 Then Exit Function
//!
//! Dim code As Long
//! code = AscW(char)
//!
//! IsInUnicodeRange = (code >= rangeStart And code <= rangeEnd)
//! End Function
//!
//! ' Example usage:
//! ' If IsInUnicodeRange(char, 0x4E00, 0x9FFF) Then
//! ' Debug.Print "CJK Unified Ideograph"
//! ' End If
//! ```
//!
//! ### Unicode Text File Writer
//! ```vb
//! Sub WriteUnicodeFile(filename As String, text As String)
//! Dim fso As Object, stream As Object
//! Set fso = CreateObject("Scripting.FileSystemObject")
//! Set stream = CreateObject("ADODB.Stream")
//!
//! stream.Type = 2 ' Text
//! stream.Charset = "UTF-8"
//! stream.Open
//!
//! ' Add BOM for UTF-8
//! stream.WriteText ChrW(65279) ' UTF-8 BOM
//! stream.WriteText text
//!
//! stream.SaveToFile filename, 2
//! stream.Close
//! End Sub
//! ```
//!
//! ## Error Handling
//!
//! ```vb
//! Function SafeChrW(charcode As Long) As String
//! On Error GoTo ErrorHandler
//!
//! ' Normalize negative values
//! If charcode < 0 Then
//! charcode = 65536 + charcode
//! End If
//!
//! If charcode >= 0 And charcode <= 65535 Then
//! SafeChrW = ChrW(charcode)
//! Else
//! SafeChrW = "?" ' Replacement character for invalid codes
//! End If
//! Exit Function
//!
//! ErrorHandler:
//! SafeChrW = "?"
//! End Function
//! ```
//!
//! ## Performance Notes
//!
//! - `ChrW` is a fast operation with minimal overhead
//! - When building long Unicode strings, use string concatenation efficiently or a `StringBuilder` pattern
//! - For repeated character creation, consider caching the result
//! - `AscW` is the inverse function of `ChrW`
//!
//! ## Best Practices
//!
//! 1. **Use `ChrW` for Unicode** - Always use `ChrW` instead of `Chr` when working with international text
//! 2. **Validate ranges** - Check that character codes are within valid Unicode ranges
//! 3. **Handle errors** - Wrap `ChrW` calls in error handlers when processing user input
//! 4. **Document character codes** - Use constants or comments to explain non-obvious character codes
//! 5. **Test with actual data** - Verify Unicode text displays correctly in target environments
//! 6. **Consider normalization** - Be aware that some characters can be represented multiple ways
//! 7. **Use UTF-8 for files** - When saving Unicode text, prefer UTF-8 encoding
//!
//! ## Comparison with Related Functions
//!
//! | Function | Character Set | Range | Use Case |
//! |----------|---------------|-------|----------|
//! | `Chr` | System default (ANSI/Unicode) | 0-255 | Legacy code, simple ASCII |
//! | `ChrB` | ANSI (byte) | 0-255 | ANSI text, byte operations |
//! | `ChrW` | Unicode (wide) | 0-65535 | International text, symbols |
//! | `AscW` | Unicode (inverse) | Returns 0-65535 | Get Unicode code from character |
//!
//! ## Unicode Ranges Reference
//!
//! Some common Unicode ranges that work with `ChrW`:
//!
//! - **Basic Latin**: 0-127 (ASCII)
//! - **Latin-1 Supplement**: 128-255
//! - **Greek and Coptic**: 880-1023
//! - **Cyrillic**: 1024-1279
//! - **Hebrew**: 1424-1535
//! - **Arabic**: 1536-1791
//! - **CJK Unified Ideographs**: 19968-40959
//! - **Hangul Syllables**: 44032-55203
//! - **Currency Symbols**: 8352-8399
//! - **Mathematical Operators**: 8704-8959
//! - **Arrows**: 8592-8703
//! - **Box Drawing**: 9472-9599
//!
//! ## Platform Notes
//!
//! - VB6 supports Unicode through `ChrW` but stores strings internally in the system's native format
//! - On Windows NT-based systems, strings are stored as Unicode (UTF-16)
//! - On Windows 95/98/ME, strings use ANSI encoding, which may cause issues with characters outside the current code page
//! - When running on older systems, test thoroughly with non-ASCII characters
//! - Modern Windows systems (XP and later) handle Unicode properly
//!
//! ## Limitations
//!
//! - `ChrW` only supports the Basic Multilingual Plane (BMP), code points 0-65535
//! - Characters outside the BMP (like some emoji) require surrogate pairs in VB6
//! - Surrogate pairs are complex and require combining two `ChrW` calls
//! - Not all fonts support all Unicode characters; display depends on available fonts
//! - Some Unicode features like combining characters may not render correctly in all VB6 controls
use crate::;
use chrw_dollar;
/// Returns the Unicode character associated with the specified code.
///
/// `charcode` is in the range -32768 to 65535; negative values are treated as
/// `65536 + charcode`, matching VB6's 16-bit integer behavior. Code 0 returns
/// the null character (U+0000).
///
/// `ChrW` is the Variant-returning counterpart of `ChrW$`; a `Null` charcode
/// propagates as `Null`.
///
/// # Errors
///
/// Returns error 5 (`Invalid procedure call or argument`) when `charcode` is
/// outside the range -32768 to 65535, or names a UTF-16 surrogate (which cannot
/// be represented as a single Rust `char`).