Skip to content

Commit 0ae97a0

Browse files
committed
feat: Enhance HTTP and string handling, improve control flow parsing
- Updated HTTP client to correctly parse domain from URL - Improved string `split` method to handle edge cases - Extended control flow parsing to support `while let` syntax - Refined diagnostic messages for `if/while let` conditions - Refactored condition pre-processing for `if` and `while` statements
1 parent ff6cb73 commit 0ae97a0

File tree

7 files changed

+237
-170
lines changed

7 files changed

+237
-170
lines changed

planglib/std/string.pi

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,48 @@ pub trait StringExt {
1313

1414
impl StringExt for string {
1515
fn split(s:string) [string] {
16+
// 如果分隔符为空,直接返回原字符串作为唯一元素
17+
if s.byte_len() == 0 {
18+
return [*self];
19+
}
20+
1621
let chars = self.chars();
1722
let subchars = s.chars();
1823
let re = arr::new<string>();
1924
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+
}
2337
j = j + 1;
2438
}
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;
2950
}
30-
i = i + 1;
3151
}
52+
53+
// 添加最后一个子字符串
54+
if start < arr_len(chars) {
55+
re.push(self.slice(start, arr_len(chars) - start));
56+
}
57+
3258
return re.get_slice();
3359
}
3460
/// # index_of

planglib/std/task/http.pi

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,18 @@ impl HttpClient {
3131
data: [u8*1024;],
3232
len: 0,
3333
};
34+
let domain = url.split("://")[1].split("/")[0];
35+
3436
sb.add_str("GET / HTTP/1.1\r\n");
3537
sb.add_str("Host: ");
36-
sb.add_str(url);
38+
sb.add_str(domain);
3739
sb.add_str("\r\n");
3840
sb.add_str("Connection: close\r\n\r\n");
3941

4042
let request = sb.str();
4143
let bytes = request.bytes();
4244
let resolver = dns::new_dns_resolver();
43-
let ip = await resolver.resolve_async(url);
45+
let ip = await resolver.resolve_async(domain);
4446
let status = await self.socket.connect_async(ip, 80 as i32);
4547
// 发送请求
4648
await self.socket.write_async(bytes);
@@ -78,6 +80,7 @@ impl HttpClient {
7880
// 解析状态码
7981
let status_str = string_from_bytes(arr_slice(response_buf, 9, 12));
8082

83+
// TODO: 解析状态码
8184
return HttpResponse{
8285
status_code: 200 as i32,
8386
headers: headers,
@@ -115,12 +118,8 @@ impl HttpResponse {
115118
pub async fn read_all_async() Task<[u8]> {
116119
let content = arr::new<u8>();
117120
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)));
124123
}
125124
return content.get_slice();
126125
}

src/ast/diag.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ define_diag!(
182182
MACRO_EXPANSION_FAILED = "macro expansion failed",
183183
SYNTAX_ERROR_FUNC_PARAM = "syntax error: function parameter",
184184
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",
187187
EXPECT_GENERIC_TYPE = "expect generic type",
188188
EXPECT_TAILING_SYMBOL = "expect tailing symbol `!` or `?`",
189189
);

0 commit comments

Comments
 (0)