domain-key 0.5.1

High-performance, domain-driven, type-safe key system for Rust
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Utility functions and helper types for domain-key
//!
//! This module contains internal utility functions used throughout the library,
//! including optimized string operations, caching utilities, and performance helpers.

use smartstring::alias::String as SmartString;

#[cfg(not(feature = "std"))]
use alloc::borrow::Cow;
#[cfg(not(feature = "std"))]
use alloc::string::{String, ToString};
#[cfg(feature = "std")]
use std::borrow::Cow;

// ============================================================================
// STRING MANIPULATION UTILITIES
// ============================================================================

/// Add a prefix to a string with optimized allocation
///
/// This function efficiently adds a prefix to a string by pre-calculating
/// the required capacity and performing a single allocation.
///
/// # Arguments
///
/// * `key` - The original string
/// * `prefix` - The prefix to add
///
/// # Returns
///
/// A new `SmartString` with the prefix added
#[must_use]
pub fn add_prefix_optimized(key: &str, prefix: &str) -> SmartString {
    let total = prefix.len() + key.len();
    if total <= 23 {
        // Fits inline in SmartString — no heap allocation
        let mut result = SmartString::new();
        result.push_str(prefix);
        result.push_str(key);
        result
    } else {
        let mut s = String::with_capacity(total);
        s.push_str(prefix);
        s.push_str(key);
        SmartString::from(s)
    }
}

/// Add a suffix to a string with optimized allocation
///
/// This function efficiently adds a suffix to a string by pre-calculating
/// the required capacity and performing a single allocation.
///
/// # Arguments
///
/// * `key` - The original string
/// * `suffix` - The suffix to add
///
/// # Returns
///
/// A new `SmartString` with the suffix added
#[must_use]
pub fn add_suffix_optimized(key: &str, suffix: &str) -> SmartString {
    let total = key.len() + suffix.len();
    if total <= 23 {
        let mut result = SmartString::new();
        result.push_str(key);
        result.push_str(suffix);
        result
    } else {
        let mut s = String::with_capacity(total);
        s.push_str(key);
        s.push_str(suffix);
        SmartString::from(s)
    }
}

/// Create a new split cache for consistent API
///
/// This function creates a split iterator that can be used consistently
/// across different optimization levels.
///
/// # Arguments
///
/// * `s` - The string to split
/// * `delimiter` - The character to split on
///
/// # Returns
///
/// A split iterator over the string
#[inline]
#[must_use]
pub fn new_split_cache(s: &str, delimiter: char) -> core::str::Split<'_, char> {
    s.split(delimiter)
}

/// Join string parts with a delimiter, optimizing for common cases
///
/// This function efficiently joins string parts using pre-calculated sizing
/// to minimize allocations.
///
/// # Arguments
///
/// * `parts` - The string parts to join
/// * `delimiter` - The delimiter to use between parts
///
/// # Returns
///
/// A new string with all parts joined
#[must_use]
pub fn join_optimized(parts: &[&str], delimiter: &str) -> String {
    if parts.is_empty() {
        return String::new();
    }

    if parts.len() == 1 {
        return parts[0].to_string();
    }

    // Calculate total capacity needed
    let total_content_len: usize = parts.iter().map(|s| s.len()).sum();
    let delimiter_len = delimiter.len() * (parts.len().saturating_sub(1));
    let total_capacity = total_content_len + delimiter_len;

    let mut result = String::with_capacity(total_capacity);

    for (i, part) in parts.iter().enumerate() {
        if i > 0 {
            result.push_str(delimiter);
        }
        result.push_str(part);
    }

    result
}

/// Count the number of occurrences of a character in a string
///
/// This function efficiently counts character occurrences without
/// allocating intermediate collections. Uses byte-level iteration
/// for ASCII characters.
///
/// # Arguments
///
/// * `s` - The string to search
/// * `target` - The character to count
///
/// # Returns
///
/// The number of times the character appears in the string
#[cfg(test)]
#[must_use]
pub fn count_char(s: &str, target: char) -> usize {
    if target.is_ascii() {
        let byte = target as u8;
        #[expect(
            clippy::naive_bytecount,
            reason = "intentional simple byte scan — fast enough for ASCII char counting"
        )]
        s.as_bytes().iter().filter(|&&b| b == byte).count()
    } else {
        s.chars().filter(|&c| c == target).count()
    }
}

/// Find the position of the nth occurrence of a character
///
/// This function finds the byte position of the nth occurrence of a character
/// in a string, useful for caching split positions.
///
/// # Arguments
///
/// * `s` - The string to search
/// * `target` - The character to find
/// * `n` - Which occurrence to find (0-based)
///
/// # Returns
///
/// The byte position of the nth occurrence, or `None` if not found
#[cfg(test)]
#[must_use]
pub fn find_nth_char(s: &str, target: char, n: usize) -> Option<usize> {
    let mut count = 0;
    for (pos, c) in s.char_indices() {
        if c == target {
            if count == n {
                return Some(pos);
            }
            count += 1;
        }
    }
    None
}

// ============================================================================
// NORMALIZATION UTILITIES
// ============================================================================

/// Trim whitespace and normalize case efficiently
///
/// This function combines trimming and case normalization in a single pass
/// when possible.
///
/// # Arguments
///
/// * `s` - The string to normalize
/// * `to_lowercase` - Whether to convert to lowercase
///
/// # Returns
///
/// A normalized string, borrowing when no changes are needed
#[must_use]
pub fn normalize_string(s: &str, to_lowercase: bool) -> Cow<'_, str> {
    let trimmed = s.trim();
    let needs_trim = trimmed.len() != s.len();
    let needs_lowercase = to_lowercase && trimmed.chars().any(|c| c.is_ascii_uppercase());

    match (needs_trim, needs_lowercase) {
        (false, false) => Cow::Borrowed(s),
        (true, false) => Cow::Borrowed(trimmed),
        (_, true) => Cow::Owned(trimmed.to_ascii_lowercase()),
    }
}

/// Replace characters efficiently with a mapping function
///
/// This function applies character replacements without unnecessary allocations
/// when no replacements are needed. Uses a single-pass algorithm that borrows
/// when no changes are needed and only allocates on first replacement found.
///
/// # Arguments
///
/// * `s` - The input string
/// * `replacer` - Function that maps characters to their replacements
///
/// # Returns
///
/// A string with replacements applied, borrowing when no changes are needed
pub fn replace_chars<F>(s: &str, replacer: F) -> Cow<'_, str>
where
    F: Fn(char) -> Option<char>,
{
    // Single-pass: only allocate when we find the first replacement
    let mut chars = s.char_indices();
    while let Some((i, c)) = chars.next() {
        if let Some(replacement) = replacer(c) {
            // Found first replacement — allocate and copy prefix, then continue
            let mut result = String::with_capacity(s.len());
            result.push_str(&s[..i]);
            result.push(replacement);
            for (_, c) in chars {
                if let Some(r) = replacer(c) {
                    result.push(r);
                } else {
                    result.push(c);
                }
            }
            return Cow::Owned(result);
        }
    }
    Cow::Borrowed(s)
}

// ============================================================================
// VALIDATION UTILITIES
// ============================================================================

/// Fast character class checking using lookup tables
///
/// This module provides optimized character validation functions using
/// precomputed lookup tables for common character classes.
#[expect(
    clippy::cast_possible_truncation,
    reason = "index is always < 128 so truncation from usize to u8 is safe"
)]
pub mod char_validation {
    /// Lookup table for ASCII alphanumeric characters
    const ASCII_ALPHANUMERIC: [bool; 128] = {
        let mut table = [false; 128];
        let mut i = 0;
        while i < 128 {
            table[i] = matches!(i as u8, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z');
            i += 1;
        }
        table
    };

    const KEY_CHARS: [bool; 128] = {
        let mut table = [false; 128];
        let mut i = 0;
        while i < 128 {
            table[i] =
                matches!(i as u8,  b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' | b'_' | b'-' | b'.');
            i += 1;
        }
        table
    };

    /// Fast check if a character is ASCII alphanumeric
    #[inline]
    #[must_use]
    pub fn is_ascii_alphanumeric_fast(c: char) -> bool {
        if c.is_ascii() {
            ASCII_ALPHANUMERIC[c as u8 as usize]
        } else {
            false
        }
    }

    /// Fast check if a character is allowed in keys
    #[inline]
    #[must_use]
    pub fn is_key_char_fast(c: char) -> bool {
        if c.is_ascii() {
            KEY_CHARS[c as u8 as usize]
        } else {
            false
        }
    }

    /// Check if a character is a common separator
    #[inline]
    #[must_use]
    pub fn is_separator(c: char) -> bool {
        matches!(c, '_' | '-' | '.' | '/' | ':' | '|')
    }

    /// Check if a character is whitespace (space, tab, newline, etc.)
    #[inline]
    #[must_use]
    pub fn is_whitespace_fast(c: char) -> bool {
        matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0B' | '\x0C')
    }
}

// ============================================================================
// FEATURE DETECTION
// ============================================================================

/// Returns the name of the active hash algorithm
///
/// The algorithm is selected at compile time based on feature flags:
/// - `fast` — `GxHash` (requires AES-NI), falls back to `AHash`
/// - `secure` — `AHash` (`DoS`-resistant)
/// - `crypto` — Blake3 (cryptographic)
/// - default — `DefaultHasher` (std) or FNV-1a (`no_std`)
///
/// # Examples
///
/// ```rust
/// let algo = domain_key::hash_algorithm();
/// println!("Using hash algorithm: {algo}");
/// ```
#[must_use]
pub const fn hash_algorithm() -> &'static str {
    #[cfg(feature = "fast")]
    {
        #[cfg(any(
            all(target_arch = "x86_64", target_feature = "aes"),
            all(target_arch = "aarch64", target_feature = "aes")
        ))]
        return "GxHash";

        #[cfg(not(any(
            all(target_arch = "x86_64", target_feature = "aes"),
            all(target_arch = "aarch64", target_feature = "aes")
        )))]
        return "AHash (GxHash fallback)";
    }

    #[cfg(all(feature = "secure", not(feature = "fast")))]
    return "AHash";

    #[cfg(all(feature = "crypto", not(any(feature = "fast", feature = "secure"))))]
    return "Blake3";

    #[cfg(not(any(feature = "fast", feature = "secure", feature = "crypto")))]
    {
        #[cfg(feature = "std")]
        return "DefaultHasher";

        #[cfg(not(feature = "std"))]
        return "FNV-1a";
    }
}

// ============================================================================
// TESTS
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    use crate::ValidationResult;
    #[cfg(not(feature = "std"))]
    use alloc::vec;
    #[cfg(not(feature = "std"))]
    use alloc::vec::Vec;

    #[test]
    fn test_add_prefix_suffix() {
        let result = add_prefix_optimized("test", "prefix_");
        assert_eq!(result, "prefix_test");

        let result = add_suffix_optimized("test", "_suffix");
        assert_eq!(result, "test_suffix");
    }

    #[test]
    fn test_join_optimized() {
        let parts = vec!["a", "b", "c"];
        let result = join_optimized(&parts, "_");
        assert_eq!(result, "a_b_c");

        let empty: Vec<&str> = vec![];
        let result = join_optimized(&empty, "_");
        assert_eq!(result, "");

        let single = vec!["alone"];
        let result = join_optimized(&single, "_");
        assert_eq!(result, "alone");
    }

    #[test]
    fn test_char_validation() {
        use char_validation::*;

        assert!(is_ascii_alphanumeric_fast('a'));
        assert!(is_ascii_alphanumeric_fast('Z'));
        assert!(is_ascii_alphanumeric_fast('5'));
        assert!(!is_ascii_alphanumeric_fast('_'));
        assert!(!is_ascii_alphanumeric_fast('ñ'));

        assert!(is_key_char_fast('a'));
        assert!(is_key_char_fast('_'));
        assert!(is_key_char_fast('-'));
        assert!(is_key_char_fast('.'));
        assert!(!is_key_char_fast(' '));

        assert!(is_separator('_'));
        assert!(is_separator('/'));
        assert!(!is_separator('a'));

        assert!(is_whitespace_fast(' '));
        assert!(is_whitespace_fast('\t'));
        assert!(!is_whitespace_fast('a'));
    }

    #[test]
    fn test_string_utilities() {
        assert_eq!(count_char("hello_world_test", '_'), 2);
        assert_eq!(count_char("no_underscores", '_'), 1);

        assert_eq!(find_nth_char("a_b_c_d", '_', 0), Some(1));
        assert_eq!(find_nth_char("a_b_c_d", '_', 1), Some(3));
        assert_eq!(find_nth_char("a_b_c_d", '_', 2), Some(5));
        assert_eq!(find_nth_char("a_b_c_d", '_', 3), None);
    }

    #[test]
    fn test_normalize_string() {
        let result = normalize_string("  Hello  ", true);
        assert_eq!(result, "hello");

        let result = normalize_string("hello", true);
        assert_eq!(result, "hello");

        let result = normalize_string("  hello  ", false);
        assert_eq!(result, "hello");

        let result = normalize_string("hello", false);
        assert!(matches!(result, Cow::Borrowed("hello")));
    }

    #[test]
    fn test_float_comparison() {
        const EPSILON: f64 = 1e-10;
        let result = ValidationResult {
            total_processed: 2,
            valid: vec!["key1".to_string(), "key2".to_string()],
            errors: vec![],
        };

        // Use approximate comparison for floats

        assert!((result.success_rate() - 100.0).abs() < EPSILON);
    }

    #[test]
    fn test_replace_chars() {
        let result = replace_chars("hello-world", |c| if c == '-' { Some('_') } else { None });
        assert_eq!(result, "hello_world");

        let result = replace_chars("hello_world", |c| if c == '-' { Some('_') } else { None });
        assert!(matches!(result, Cow::Borrowed("hello_world")));
    }

    #[test]
    fn test_replace_chars_fixed() {
        let result = replace_chars("hello-world", |c| if c == '-' { Some('_') } else { None });
        assert_eq!(result, "hello_world");

        let result = replace_chars("hello_world", |c| if c == '-' { Some('_') } else { None });
        assert!(matches!(result, Cow::Borrowed("hello_world")));

        // Test with multiple replacements
        let result = replace_chars("a-b-c", |c| if c == '-' { Some('_') } else { None });
        assert_eq!(result, "a_b_c");

        // Test with no replacements needed
        let result = replace_chars("hello", |c| if c == 'x' { Some('y') } else { None });
        assert!(matches!(result, Cow::Borrowed(_)));

        // Test empty string
        let result = replace_chars("", |c| if c == 'x' { Some('y') } else { None });
        assert_eq!(result, "");
    }
}