@@ -42,9 +42,9 @@ pub fn encoder_set_bracketed(encoder_: Encoder, enabled: bool) callconv(.c) void
4242}
4343
4444pub fn encode (
45+ encoder_ : Encoder ,
4546 data_ : ? [* ]u8 ,
4647 len : usize ,
47- encoder_ : Encoder ,
4848 out_ : ? [* ]u8 ,
4949 out_len : usize ,
5050 out_written_ : ? * usize ,
@@ -74,16 +74,6 @@ pub fn is_safe(data: ?[*]const u8, len: usize) callconv(.c) bool {
7474 return paste .isSafe (slice );
7575}
7676
77- test "alloc paste encoder" {
78- const testing = std .testing ;
79- var e : Encoder = undefined ;
80- try testing .expectEqual (Result .success , new (
81- & lib_alloc .test_allocator ,
82- & e ,
83- ));
84- free (e );
85- }
86-
8777test "is_safe with safe data" {
8878 const testing = std .testing ;
8979 const safe = "hello world" ;
@@ -112,3 +102,83 @@ test "is_safe with null empty data" {
112102 const testing = std .testing ;
113103 try testing .expect (is_safe (null , 0 ));
114104}
105+
106+ test "encode bracketed" {
107+ const testing = std .testing ;
108+ const alloc = testing .allocator ;
109+
110+ var encoder : Encoder = undefined ;
111+ try testing .expect (new (& lib_alloc .test_allocator , & encoder ) == .success );
112+ encoder_set_bracketed (encoder , true );
113+
114+ const text : []u8 = try alloc .dupe (u8 , "hello" );
115+ defer alloc .free (text );
116+
117+ var out : [128 ]u8 = undefined ;
118+ var out_len : usize = 0 ;
119+
120+ try testing .expect (encode (encoder , text .ptr , text .len , & out , out .len , & out_len ) == .success );
121+ try testing .expectEqualStrings ("\x1b [200~hello\x1b [201~" , out [0 .. out_len - 1 ]);
122+
123+ free (encoder );
124+ }
125+
126+ test "encode unbracketed no newlines" {
127+ const testing = std .testing ;
128+ const alloc = testing .allocator ;
129+
130+ var encoder : Encoder = undefined ;
131+ try testing .expect (new (& lib_alloc .test_allocator , & encoder ) == .success );
132+ encoder_set_bracketed (encoder , false );
133+
134+ const text : []u8 = try alloc .dupe (u8 , "hello" );
135+ defer alloc .free (text );
136+
137+ var out : [128 ]u8 = undefined ;
138+ var out_len : usize = 0 ;
139+
140+ try testing .expect (encode (encoder , text .ptr , text .len , & out , out .len , & out_len ) == .success );
141+ try testing .expectEqualStrings ("hello" , out [0 .. out_len - 1 ]);
142+
143+ free (encoder );
144+ }
145+
146+ test "encode unbracketed newlines" {
147+ const testing = std .testing ;
148+ const alloc = testing .allocator ;
149+
150+ var encoder : Encoder = undefined ;
151+ try testing .expect (new (& lib_alloc .test_allocator , & encoder ) == .success );
152+ encoder_set_bracketed (encoder , false );
153+
154+ const text : []u8 = try alloc .dupe (u8 , "hello\n world" );
155+ defer alloc .free (text );
156+
157+ var out : [128 ]u8 = undefined ;
158+ var out_len : usize = 0 ;
159+
160+ try testing .expect (encode (encoder , text .ptr , text .len , & out , out .len , & out_len ) == .success );
161+ try testing .expectEqualStrings ("hello\r world" , out [0 .. out_len - 1 ]);
162+
163+ free (encoder );
164+ }
165+
166+ test "encode unbracketed windows-style newline" {
167+ const testing = std .testing ;
168+ const alloc = testing .allocator ;
169+
170+ var encoder : Encoder = undefined ;
171+ try testing .expect (new (& lib_alloc .test_allocator , & encoder ) == .success );
172+ encoder_set_bracketed (encoder , false );
173+
174+ const text : []u8 = try alloc .dupe (u8 , "hello\r \n world" );
175+ defer alloc .free (text );
176+
177+ var out : [128 ]u8 = undefined ;
178+ var out_len : usize = 0 ;
179+
180+ try testing .expect (encode (encoder , text .ptr , text .len , & out , out .len , & out_len ) == .success );
181+ try testing .expectEqualStrings ("hello\r\r world" , out [0 .. out_len - 1 ]);
182+
183+ free (encoder );
184+ }
0 commit comments