File tree Expand file tree Collapse file tree 7 files changed +237
-170
lines changed Expand file tree Collapse file tree 7 files changed +237
-170
lines changed Original file line number Diff line number Diff line change @@ -13,22 +13,48 @@ pub trait StringExt {
13
13
14
14
impl StringExt for string {
15
15
fn split(s:string) [string] {
16
+ // 如果分隔符为空,直接返回原字符串作为唯一元素
17
+ if s.byte_len() == 0 {
18
+ return [*self];
19
+ }
20
+
16
21
let chars = self.chars();
17
22
let subchars = s.chars();
18
23
let re = arr::new<string>();
19
24
let i = 0;
20
- let j = 0;
21
- while i < arr_len(chars) {
22
- if chars[i] == subchars[j] {
25
+ let start = 0;
26
+
27
+ while i <= arr_len(chars) - arr_len(subchars) {
28
+ let m = true;
29
+ let j = 0;
30
+
31
+ // 检查当前位置是否匹配分隔符
32
+ while j < arr_len(subchars) {
33
+ if chars[i + j] != subchars[j] {
34
+ m = false;
35
+ break;
36
+ }
23
37
j = j + 1;
24
38
}
25
- if j == arr_len(subchars) {
26
- let sub = self.slice(i, j);
27
- re.push(sub);
28
- j = 0;
39
+
40
+ // 如果找到匹配
41
+ if m {
42
+ // 添加分隔符前的子字符串
43
+ re.push(self.slice(start, i - start));
44
+ // 跳过分隔符
45
+ i = i + arr_len(subchars);
46
+ // 更新下一个子字符串的起始位置
47
+ start = i;
48
+ } else {
49
+ i = i + 1;
29
50
}
30
- i = i + 1;
31
51
}
52
+
53
+ // 添加最后一个子字符串
54
+ if start < arr_len(chars) {
55
+ re.push(self.slice(start, arr_len(chars) - start));
56
+ }
57
+
32
58
return re.get_slice();
33
59
}
34
60
/// # index_of
Original file line number Diff line number Diff line change @@ -31,16 +31,18 @@ impl HttpClient {
31
31
data: [u8*1024;],
32
32
len: 0,
33
33
};
34
+ let domain = url.split("://")[1].split("/")[0];
35
+
34
36
sb.add_str("GET / HTTP/1.1\r\n");
35
37
sb.add_str("Host: ");
36
- sb.add_str(url );
38
+ sb.add_str(domain );
37
39
sb.add_str("\r\n");
38
40
sb.add_str("Connection: close\r\n\r\n");
39
41
40
42
let request = sb.str();
41
43
let bytes = request.bytes();
42
44
let resolver = dns::new_dns_resolver();
43
- let ip = await resolver.resolve_async(url );
45
+ let ip = await resolver.resolve_async(domain );
44
46
let status = await self.socket.connect_async(ip, 80 as i32);
45
47
// 发送请求
46
48
await self.socket.write_async(bytes);
@@ -78,6 +80,7 @@ impl HttpClient {
78
80
// 解析状态码
79
81
let status_str = string_from_bytes(arr_slice(response_buf, 9, 12));
80
82
83
+ // TODO: 解析状态码
81
84
return HttpResponse{
82
85
status_code: 200 as i32,
83
86
headers: headers,
@@ -115,12 +118,8 @@ impl HttpResponse {
115
118
pub async fn read_all_async() Task<[u8]> {
116
119
let content = arr::new<u8>();
117
120
let buf = [u8*4096;];
118
- while true {
119
- if let n = (await self.read_async(buf)) as i64 {
120
- content.append(arr::from_slice(arr_slice(buf, 0, n)));
121
- } else {
122
- break;
123
- }
121
+ while let n = (await self.read_async(buf)) as i64 {
122
+ content.append(arr::from_slice(buf.slice(0, n)));
124
123
}
125
124
return content.get_slice();
126
125
}
Original file line number Diff line number Diff line change @@ -182,8 +182,8 @@ define_diag!(
182
182
MACRO_EXPANSION_FAILED = "macro expansion failed" ,
183
183
SYNTAX_ERROR_FUNC_PARAM = "syntax error: function parameter" ,
184
184
ONLY_AWAIT_IN_ASYNC_FN = "await is only expected in async functions" ,
185
- IF_LET_DOES_NOT_EXPECT_TAIL = "`if let ...` does not expect `?` or `!` tail" ,
186
- EXPECT_IF_LET_AS = "`if let .. as ...` is expected here" ,
185
+ IF_LET_DOES_NOT_EXPECT_TAIL = "`if/while let ...` does not expect `?` or `!` tail" ,
186
+ EXPECT_IF_LET_AS = "`if/while let .. as ...` is expected here" ,
187
187
EXPECT_GENERIC_TYPE = "expect generic type" ,
188
188
EXPECT_TAILING_SYMBOL = "expect tailing symbol `!` or `?`" ,
189
189
) ;
You can’t perform that action at this time.
0 commit comments