-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.rs
More file actions
173 lines (157 loc) · 4.77 KB
/
Copy pathstring.rs
File metadata and controls
173 lines (157 loc) · 4.77 KB
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
use crate::{Value, Result};
use std::fmt;
/// Candle String 包装
///
/// 提供对 Candle 字符串的 Rust 友好访问接口。
pub struct CandleString {
value: Value,
}
impl CandleString {
/// 从 Value 创建(内部使用)
pub(crate) fn from_value(value: Value) -> Self {
CandleString { value }
}
/// 转换为 Value
pub fn into_value(self) -> Value {
self.value
}
/// 获取字符串长度(Unicode 标量值数量)
///
/// # 示例
///
/// ```no_run
/// # use candle::Value;
/// let s = Value::string("Hello");
/// let candle_str = s.as_string().unwrap();
/// assert_eq!(candle_str.len(), 5);
/// ```
pub fn len(&self) -> usize {
unsafe {
candle_rt_string_length(self.value.as_raw()) as usize
}
}
/// 检查是否为空字符串
pub fn is_empty(&self) -> bool {
self.len() == 0
}
/// 转换为 Rust String
///
/// # 示例
///
/// ```no_run
/// # use candle::Value;
/// let s = Value::string("Hello, Candle!");
/// let candle_str = s.as_string().unwrap();
/// assert_eq!(candle_str.to_string(), "Hello, Candle!");
/// ```
pub fn to_string(&self) -> String {
unsafe {
// 获取 UTF-8 字节指针和长度
let len = self.len();
if len == 0 {
return String::new();
}
// 注意:这里假设运行时提供了获取 UTF-8 字节的接口
// 实际实现可能需要调整
let mut buffer = vec![0u8; len];
candle_rt_string_to_utf8(
self.value.as_raw(),
buffer.as_mut_ptr() as *mut i8,
len
);
String::from_utf8_lossy(&buffer).to_string()
}
}
/// 获取子字符串
///
/// # 参数
///
/// * `start` - 起始位置(包含)
/// * `end` - 结束位置(不包含)
///
/// # 示例
///
/// ```no_run
/// # use candle::Value;
/// let s = Value::string("Hello, World!");
/// let candle_str = s.as_string().unwrap();
/// let sub = candle_str.substring(0, 5).unwrap();
/// assert_eq!(sub.to_string(), "Hello");
/// ```
pub fn substring(&self, start: usize, end: usize) -> Result<CandleString> {
unsafe {
let result = candle_rt_string_substring(
self.value.as_raw(),
start as i64,
end as i64
);
Ok(CandleString::from_value(Value::from_raw(result)))
}
}
/// 检查是否包含子串
pub fn contains(&self, needle: &str) -> bool {
unsafe {
let needle_val = Value::string(needle);
candle_rt_string_contains(
self.value.as_raw(),
needle_val.as_raw()
)
}
}
/// 检查是否以指定前缀开头
pub fn starts_with(&self, prefix: &str) -> bool {
unsafe {
let prefix_val = Value::string(prefix);
candle_rt_string_starts_with(
self.value.as_raw(),
prefix_val.as_raw()
)
}
}
/// 检查是否以指定后缀结尾
pub fn ends_with(&self, suffix: &str) -> bool {
unsafe {
let suffix_val = Value::string(suffix);
candle_rt_string_ends_with(
self.value.as_raw(),
suffix_val.as_raw()
)
}
}
/// 获取指定索引处的字符
pub fn char_at(&self, index: usize) -> Result<char> {
unsafe {
let code = candle_rt_string_code_point_at(
self.value.as_raw(),
index as i64
);
if code < 0 {
return Err(crate::Error::IndexOutOfBounds {
index: index as i64,
length: self.len(),
});
}
Ok(char::from_u32(code as u32).unwrap_or('\0'))
}
}
}
impl fmt::Display for CandleString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
impl fmt::Debug for CandleString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CandleString({:?})", self.to_string())
}
}
// 外部函数声明
extern "C" {
fn candle_rt_string_length(ptr: i64) -> i64;
fn candle_rt_string_to_utf8(ptr: i64, buf: *mut i8, len: usize);
fn candle_rt_string_substring(ptr: i64, start: i64, end: i64) -> i64;
fn candle_rt_string_contains(ptr: i64, needle: i64) -> bool;
fn candle_rt_string_starts_with(ptr: i64, prefix: i64) -> bool;
fn candle_rt_string_ends_with(ptr: i64, suffix: i64) -> bool;
fn candle_rt_string_code_point_at(ptr: i64, index: i64) -> i64;
}