diff options
| author | 2025-08-14 21:16:46 +0200 | |
|---|---|---|
| committer | 2025-08-16 09:35:10 +0200 | |
| commit | a05e82841740fbc3a79474aa88b107d86392f948 (patch) | |
| tree | af3d90d26a6de9fd5df7c08d3af86b37f194cd4f /clap.zig | |
| parent | fix: Typo in `HelpOptions.markdown_lite` doc comment (diff) | |
| download | zig-clap-a05e82841740fbc3a79474aa88b107d86392f948.tar.gz zig-clap-a05e82841740fbc3a79474aa88b107d86392f948.tar.xz zig-clap-a05e82841740fbc3a79474aa88b107d86392f948.zip | |
using new arraylist api
Diffstat (limited to 'clap.zig')
| -rw-r--r-- | clap.zig | 19 |
1 files changed, 8 insertions, 11 deletions
| @@ -84,11 +84,11 @@ pub fn parseParams(allocator: std.mem.Allocator, str: []const u8) ![]Param(Help) | |||
| 84 | /// Takes a string and parses it into many Param(Help). Returned is a newly allocated slice | 84 | /// Takes a string and parses it into many Param(Help). Returned is a newly allocated slice |
| 85 | /// containing all the parsed params. The caller is responsible for freeing the slice. | 85 | /// containing all the parsed params. The caller is responsible for freeing the slice. |
| 86 | pub fn parseParamsEx(allocator: std.mem.Allocator, str: []const u8, end: *usize) ![]Param(Help) { | 86 | pub fn parseParamsEx(allocator: std.mem.Allocator, str: []const u8, end: *usize) ![]Param(Help) { |
| 87 | var list = std.ArrayList(Param(Help)).init(allocator); | 87 | var list = std.ArrayList(Param(Help)){}; |
| 88 | errdefer list.deinit(); | 88 | errdefer list.deinit(allocator); |
| 89 | 89 | ||
| 90 | try parseParamsIntoArrayListEx(&list, str, end); | 90 | try parseParamsIntoArrayListEx(allocator, &list, str, end); |
| 91 | return try list.toOwnedSlice(); | 91 | return try list.toOwnedSlice(allocator); |
| 92 | } | 92 | } |
| 93 | 93 | ||
| 94 | /// Takes a string and parses it into many Param(Help) at comptime. Returned is an array of | 94 | /// Takes a string and parses it into many Param(Help) at comptime. Returned is an array of |
| @@ -131,9 +131,7 @@ fn countParams(str: []const u8) usize { | |||
| 131 | /// is returned, containing all the parameters parsed. This function will fail if the input slice | 131 | /// is returned, containing all the parameters parsed. This function will fail if the input slice |
| 132 | /// is to small. | 132 | /// is to small. |
| 133 | pub fn parseParamsIntoSlice(slice: []Param(Help), str: []const u8) ![]Param(Help) { | 133 | pub fn parseParamsIntoSlice(slice: []Param(Help), str: []const u8) ![]Param(Help) { |
| 134 | var null_alloc = std.heap.FixedBufferAllocator.init(""); | ||
| 135 | var list = std.ArrayList(Param(Help)){ | 134 | var list = std.ArrayList(Param(Help)){ |
| 136 | .allocator = null_alloc.allocator(), | ||
| 137 | .items = slice[0..0], | 135 | .items = slice[0..0], |
| 138 | .capacity = slice.len, | 136 | .capacity = slice.len, |
| 139 | }; | 137 | }; |
| @@ -146,14 +144,13 @@ pub fn parseParamsIntoSlice(slice: []Param(Help), str: []const u8) ![]Param(Help | |||
| 146 | /// is returned, containing all the parameters parsed. This function will fail if the input slice | 144 | /// is returned, containing all the parameters parsed. This function will fail if the input slice |
| 147 | /// is to small. | 145 | /// is to small. |
| 148 | pub fn parseParamsIntoSliceEx(slice: []Param(Help), str: []const u8, end: *usize) ![]Param(Help) { | 146 | pub fn parseParamsIntoSliceEx(slice: []Param(Help), str: []const u8, end: *usize) ![]Param(Help) { |
| 149 | var null_alloc = std.heap.FixedBufferAllocator.init(""); | 147 | var null_allocator = std.heap.FixedBufferAllocator.init(""); |
| 150 | var list = std.ArrayList(Param(Help)){ | 148 | var list = std.ArrayList(Param(Help)){ |
| 151 | .allocator = null_alloc.allocator(), | ||
| 152 | .items = slice[0..0], | 149 | .items = slice[0..0], |
| 153 | .capacity = slice.len, | 150 | .capacity = slice.len, |
| 154 | }; | 151 | }; |
| 155 | 152 | ||
| 156 | try parseParamsIntoArrayListEx(&list, str, end); | 153 | try parseParamsIntoArrayListEx(null_allocator.allocator(), &list, str, end); |
| 157 | return list.items; | 154 | return list.items; |
| 158 | } | 155 | } |
| 159 | 156 | ||
| @@ -164,13 +161,13 @@ pub fn parseParamsIntoArrayList(list: *std.ArrayList(Param(Help)), str: []const | |||
| 164 | } | 161 | } |
| 165 | 162 | ||
| 166 | /// Takes a string and parses it into many Param(Help), which are appended onto `list`. | 163 | /// Takes a string and parses it into many Param(Help), which are appended onto `list`. |
| 167 | pub fn parseParamsIntoArrayListEx(list: *std.ArrayList(Param(Help)), str: []const u8, end: *usize) !void { | 164 | pub fn parseParamsIntoArrayListEx(allocator: std.mem.Allocator, list: *std.ArrayList(Param(Help)), str: []const u8, end: *usize) !void { |
| 168 | var i: usize = 0; | 165 | var i: usize = 0; |
| 169 | while (i != str.len) { | 166 | while (i != str.len) { |
| 170 | var end_of_this: usize = undefined; | 167 | var end_of_this: usize = undefined; |
| 171 | errdefer end.* = i + end_of_this; | 168 | errdefer end.* = i + end_of_this; |
| 172 | 169 | ||
| 173 | try list.append(try parseParamEx(str[i..], &end_of_this)); | 170 | try list.append(allocator, try parseParamEx(str[i..], &end_of_this)); |
| 174 | i += end_of_this; | 171 | i += end_of_this; |
| 175 | } | 172 | } |
| 176 | 173 | ||