-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathstring_test.ks
More file actions
67 lines (53 loc) · 1.85 KB
/
Copy pathstring_test.ks
File metadata and controls
67 lines (53 loc) · 1.85 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
// KernelScript String Type Demonstration
// Shows unified string syntax working in both eBPF and userspace contexts
include "xdp.kh"
@xdp fn string_demo(ctx: *xdp_md) -> xdp_action {
// Test string declarations with different sizes
var name: str(16) = "hello"
var message: str(32) = "world"
var large_buffer: str(128) = "large message buffer"
// Test string indexing
var first_char: char = name[0]
var second_char: char = name[1]
// Test string comparison
if (name == "hello") {
// String concatenation
var result: str(48) = name + message
// Test string inequality
if (result != "helloworld") {
return XDP_DROP
}
}
// Test smaller strings
var tiny: str(4) = "abc"
var custom: str(10) = "custom"
return XDP_PASS
}
// Userspace coordinator demonstrating the same string operations
fn main() -> i32 {
// Same string syntax works in userspace
var greeting: str(20) = "Hello"
var target: str(20) = "World"
var punctuation: str(5) = "!"
// String concatenation in userspace
var message: str(45) = greeting + target
var final_message: str(50) = message + punctuation
// String comparison in userspace
if (greeting == "Hello") {
// Character access
var first: char = greeting[0]
var last: char = target[4]
// String inequality test
if (final_message != "HelloWorld!") {
return 1
}
}
// Test string truncation behavior
var short: str(6) = "toolong" // Will be truncated to "toolo" + null
var exact: str(6) = "exact" // Fits perfectly: "exact" + null
// Demonstrate different string sizes
var tiny: str(3) = "hi" // 2 chars + null
var medium: str(32) = "medium length string"
var large: str(128) = "this is a much longer string for testing"
return 0
}