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
/// Convert Windows CHAR array to Rust String
///
/// This function converts a null-terminated Windows CHAR array (i8 slice) to a Rust String.
/// It reads bytes until it encounters a null terminator (0), then converts them to UTF-8.
///
/// # Parameters
/// - `char_array`: A slice of i8 values representing a Windows CHAR array (typically ANSI encoded)
///
/// # Returns
/// A Rust String containing the converted text. Invalid UTF-8 sequences are replaced with the Unicode replacement character (U+FFFD).
///
/// # Safety Considerations
/// - The input should be a valid null-terminated CHAR array
/// - The function assumes ANSI or ASCII encoding
/// - For Unicode text, consider using wide character (UTF-16) APIs instead
///
/// # Example
/// ```
/// use win_auto_utils::utils::char_array_to_string;
///
/// // Simulate a Windows CHAR array (null-terminated)
/// let char_array: &[i8] = &[72, 101, 108, 108, 111, 0]; // "Hello\0"
/// let result = char_array_to_string(char_array);
/// assert_eq!(result, "Hello");
///
/// // Array without explicit null terminator (will read until end)
/// let char_array2: &[i8] = &[87, 111, 114, 108, 100]; // "World"
/// let result2 = char_array_to_string(char_array2);
/// assert_eq!(result2, "World");
/// ```
///
/// # Performance Notes
/// - Creates an intermediate `Vec<u8>` for the conversion
/// - Uses `from_utf8_lossy` which handles invalid UTF-8 gracefully
/// - Suitable for occasional conversions; for hot loops, consider caching results
/// Convert Rust string to PCWSTR (UTF-16 null-terminated) - Optimized version
///
/// This function converts a Rust string to a UTF-16 encoded vector with a null terminator,
/// suitable for passing to Windows API functions that expect PCWSTR/LPCWSTR.
///
/// # Performance Optimizations
/// - Pre-allocates capacity to avoid reallocations
/// - Uses `extend()` which is optimized for iterators
///
/// # Example
/// ```
/// use win_auto_utils::utils::string_to_pcwstr;
///
/// let utf16 = string_to_pcwstr("Hello");
/// assert_eq!(utf16.len(), 6); // 5 chars + null terminator
/// assert_eq!(utf16[5], 0); // null terminated
/// ```