Skip to main content

windjammer_runtime/
strings.rs

1//! String utilities
2//!
3//! Windjammer's `std::strings` module maps to these functions.
4
5/// Convert to uppercase
6pub fn to_upper(s: &str) -> String {
7    s.to_uppercase()
8}
9
10/// Convert to lowercase
11pub fn to_lower(s: &str) -> String {
12    s.to_lowercase()
13}
14
15/// Trim whitespace
16pub fn trim(s: &str) -> String {
17    s.trim().to_string()
18}
19
20/// Split string by delimiter
21pub fn split(s: &str, delimiter: &str) -> Vec<String> {
22    s.split(delimiter).map(|s| s.to_string()).collect()
23}
24
25/// Join strings with delimiter
26pub fn join(parts: &[String], delimiter: &str) -> String {
27    parts.join(delimiter)
28}
29
30/// Check if string contains substring
31pub fn contains(s: &str, substring: &str) -> bool {
32    s.contains(substring)
33}
34
35/// Check if string starts with prefix
36pub fn starts_with(s: &str, prefix: &str) -> bool {
37    s.starts_with(prefix)
38}
39
40/// Check if string ends with suffix
41pub fn ends_with(s: &str, suffix: &str) -> bool {
42    s.ends_with(suffix)
43}
44
45/// Replace all occurrences
46pub fn replace(s: &str, from: &str, to: &str) -> String {
47    s.replace(from, to)
48}
49
50/// Get substring from start to end index (exclusive)
51pub fn substring(s: &str, start: usize, end: usize) -> String {
52    s.chars()
53        .skip(start)
54        .take(end.saturating_sub(start))
55        .collect()
56}
57
58/// Get string length
59pub fn len(s: &str) -> usize {
60    s.len()
61}
62
63/// Check if string is empty
64pub fn is_empty(s: &str) -> bool {
65    s.is_empty()
66}
67
68#[cfg(test)]
69mod tests {
70    use super::*;
71
72    #[test]
73    fn test_case_conversion() {
74        assert_eq!(to_upper("hello"), "HELLO");
75        assert_eq!(to_lower("WORLD"), "world");
76    }
77
78    #[test]
79    fn test_trim() {
80        assert_eq!(trim("  hello  "), "hello");
81    }
82
83    #[test]
84    fn test_split_join() {
85        let parts = split("a,b,c", ",");
86        assert_eq!(parts, vec!["a", "b", "c"]);
87        assert_eq!(join(&parts, "-"), "a-b-c");
88    }
89
90    #[test]
91    fn test_contains() {
92        assert!(contains("hello world", "world"));
93        assert!(!contains("hello", "goodbye"));
94    }
95
96    #[test]
97    fn test_replace() {
98        assert_eq!(replace("hello world", "world", "rust"), "hello rust");
99    }
100}