Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f33c7cb

Browse files
maj_20241223-04:45
1 parent 0c26667 commit f33c7cb

File tree

2 files changed

+15
-94
lines changed

2 files changed

+15
-94
lines changed

‎library/curse/cursed.zig‎

Lines changed: 8 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -79,71 +79,32 @@ pub const BackgroundColor = enum(u8) { // terminal's background colors
7979
pub const ZONATRB = struct { styled: [4]u32, backgr: BackgroundColor, foregr: ForegroundColor };
8080

8181
pub const iteratStr = struct {
82-
var strbuf: []const u8 = undefined;
83-
84-
/// Errors that may occur when using String
85-
pub const ErrNbrch = error{
86-
InvalideAllocBuffer,
87-
};
88-
82+
8983
pub const StringIterator = struct {
9084
buf: []u8,
9185
index: usize,
9286

93-
fn allocBuffer(size: usize) ErrNbrch![]u8 {
94-
const buf = allocatorTerm.alloc(u8, size) catch {
95-
return ErrNbrch.InvalideAllocBuffer;
96-
};
97-
return buf;
98-
}
99-
87+
10088
/// Deallocates the internal buffer
10189
pub fn deinit(self: *StringIterator) void {
10290
if (self.buf.len > 0) allocatorTerm.free(self.buf);
103-
strbuf = "";
10491
}
10592

10693
pub fn next(it: *StringIterator) ?[]const u8 {
107-
const optional_buf: ?[]u8 = allocBuffer(strbuf.len) catch return null;
108-
109-
it.buf = optional_buf orelse "";
110-
var idx: usize = 0;
111-
112-
while (true) {
113-
if (idx >= strbuf.len) break;
114-
it.buf[idx] = strbuf[idx];
115-
idx += 1;
116-
}
117-
94+
if ( it.buf.len == 0 ) return null;
95+
11896
if (it.index >= it.buf.len) return null;
119-
idx = it.index;
120-
it.index += getUTF8Size(it.buf[idx]);
121-
return it.buf[idx..it.index];
97+
consti = it.index;
98+
it.index += getUTF8Size(it.buf[i]);
99+
return it.buf[i..it.index];
122100
}
123101

124-
pub fn preview(it: *StringIterator) ?[]const u8 {
125-
const optional_buf: ?[]u8 = allocBuffer(strbuf.len) catch return null;
126-
127-
it.buf = optional_buf orelse "";
128-
var idx: usize = 0;
129-
while (true) {
130-
if (idx >= strbuf.len) break;
131-
it.buf[idx] = strbuf[idx];
132-
idx += 1;
133-
}
134-
135-
if (it.index == 0) return null;
136-
idx = it.buf.len;
137-
it.index -= getUTF8Size(it.buf[idx]);
138-
return it.buf[idx..it.index];
139-
}
140102
};
141103

142104
/// iterator String
143105
pub fn iterator(str: []const u8) StringIterator {
144-
strbuf = str;
145106
return StringIterator{
146-
.buf = undefined,
107+
.buf = allocatorTerm.dupe(u8,str) catchunreachable,
147108
.index = 0,
148109
};
149110
}

‎library/curse/utils.zig‎

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -70,74 +70,34 @@ pub fn usizeToStr(v: usize) []const u8 {
7070

7171
/// Iterator support iteration string
7272
pub const iteratStr = struct {
73-
var strbuf: []const u8 = undefined;
74-
75-
/// Errors that may occur when using String
76-
pub const ErrNbrch = error{
77-
InvalideAllocBuffer,
78-
};
7973

8074
pub const StringIterator = struct {
8175
buf: []u8,
8276
index: usize,
8377

84-
fn allocBuffer(size: usize) ErrNbrch![]u8 {
85-
const buf = allocUtl.alloc(u8, size) catch {
86-
return ErrNbrch.InvalideAllocBuffer;
87-
};
88-
return buf;
89-
}
90-
9178
/// Deallocates the internal buffer
9279
pub fn deinit(self: *StringIterator) void {
9380
if (self.buf.len > 0) allocUtl.free(self.buf);
94-
strbuf = "";
9581
self.index = 0;
9682
}
9783

9884

9985
pub fn next(it: *StringIterator) ?[]const u8 {
100-
const optional_buf: ?[]u8 = allocBuffer(strbuf.len) catch return null;
101-
102-
it.buf = optional_buf orelse "";
103-
var idx: usize = 0;
104-
while (true) {
105-
if (idx >= strbuf.len) break;
106-
it.buf[idx] = strbuf[idx];
107-
idx += 1;
108-
}
109-
86+
if ( it.buf.len == 0 ) return null;
87+
11088
if (it.index >= it.buf.len) return null;
111-
idx = it.index;
112-
it.index += getUTF8Size(it.buf[idx]);
113-
return it.buf[idx..it.index];
114-
}
115-
116-
pub fn preview(it: *StringIterator) ?[]const u8 {
117-
if (it.index == 0) return null;
118-
const optional_buf: ?[]u8 = allocBuffer(strbuf.len) catch return null;
119-
120-
it.buf = optional_buf orelse "";
121-
var idx: usize = it.index;
122-
while (true) {
123-
if (idx == 0) break;
124-
it.buf[idx] = strbuf[idx];
125-
idx -= 1;
126-
}
127-
128-
if (it.index == 0) return null;
129-
idx = it.buf.len;
130-
it.index -= getUTF8Size(it.buf[idx]);
131-
return it.buf[idx..it.index];
89+
const i = it.index;
90+
it.index += getUTF8Size(it.buf[i]);
91+
return it.buf[i..it.index];
13292
}
13393

13494
};
13595

13696
/// iterator String
13797
pub fn iterator(str: []const u8) StringIterator {
138-
strbuf=str;
98+
13999
return StringIterator{
140-
.buf = undefined,
100+
.buf = allocUtl.dupe(u8,str) catchunreachable,
141101
.index = 0,
142102
};
143103
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /