Skip to content

Commit 9f67394

Browse files
committed
keep on refactoring
1 parent b14bc7f commit 9f67394

File tree

3 files changed

+141
-168
lines changed

3 files changed

+141
-168
lines changed

src/fluent.zig

Lines changed: 13 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ const SplitIterator = fltiter.SplitIterator;
5757
const IteratorInterface = fltiter.IteratorInterface;
5858
const IteratorMode = fltiter.IteratorMode;
5959

60+
////////////////////////////////////////////////////////////////////////////////
61+
// ENUM IMPORTS ///
62+
////////////////////////////////////////////////////////////////////////////////
63+
const fltenum = @import("fluent_enum.zig");
64+
const StringMode = fltenum.StringMode;
65+
const DirectionOption = fltenum.DirectionOption;
66+
const ReplaceOption = fltenum.ReplaceOption;
67+
const TrimOptions = fltenum.TrimOptions;
68+
const SortDirection = fltenum.SortDirection;
69+
const SampleOption = fltenum.SampleOption;
70+
const FluentMode = fltenum.FluentMode;
71+
6072
////////////////////////////////////////////////////////////////////////////////
6173
// Public Fluent Interface Access Point ///
6274
////////////////////////////////////////////////////////////////////////////////
@@ -125,11 +137,6 @@ pub fn FluentInterface(comptime T: type) type {
125137
}
126138
}
127139

128-
// NOTE:
129-
// using slices here because this makes it directly
130-
// obvious that we're support any kind of slice and
131-
// both Mutable and Immutable backends.
132-
133140
///order - returns the lexicographical order compared to a given slice
134141
pub fn order(self: Self, items: []const Self.DataType) Order {
135142
return std.mem.order(Self.DataType, self.items, items);
@@ -272,9 +279,7 @@ pub fn FluentInterface(comptime T: type) type {
272279
) BaseIterator(DataType, mode) {
273280
return Fluent.iterator(mode, self.items);
274281
}
275-
////////////////////////////////////////////////////////////////////////////////
276-
/// GeneralImmutableBackend ///
277-
////////////////////////////////////////////////////////////////////////////////
282+
278283
pub fn findFrom(
279284
self: Self,
280285
comptime mode: FluentMode,
@@ -597,33 +602,6 @@ pub fn FluentInterface(comptime T: type) type {
597602
return self;
598603
}
599604

600-
};
601-
}
602-
603-
604-
const StringMode = enum { regex, scalar };
605-
606-
fn ImmutableStringBackend(comptime Self: type) type {
607-
return struct {
608-
609-
610-
////////////////////////////////////////////////////////////////////////////////
611-
// MUTABLE BACKEND : //
612-
// //
613-
// Only activated if the child data type is u8 //
614-
////////////////////////////////////////////////////////////////////////////////
615-
616-
fn MutableStringBackend(comptime Self: type) type {
617-
return struct {
618-
619-
///////////////////////
620-
// PUBLIC SECTION //
621-
///////////////////////
622-
623-
pub usingnamespace ImmutableStringBackend(Self);
624-
625-
pub usingnamespace GeneralMutableBackend(Self);
626-
627605
/// lower - transform all alphabetic characters to lower case
628606
pub fn lower(self: Self) Self {
629607
for (self.items) |*c| c.* = std.ascii.toLower(c.*);
@@ -721,142 +699,9 @@ fn MutableStringBackend(comptime Self: type) type {
721699
}
722700
return self;
723701
}
724-
725-
///////////////////////
726-
// PRIVATE SECTION //
727-
///////////////////////
728-
729702
};
730703
}
731704

732-
//////////////////////////////////////////////////////////////////////////////////
733-
// STRING BIT SET : //
734-
//////////////////////////////////////////////////////////////////////////////////
735-
736-
const StringBitSet = struct {
737-
const BackingSet = std.StaticBitSet(@bitSizeOf(usize));
738-
739-
bits: [4]BackingSet,
740-
741-
/// init - returns an initEmpty instance of StringBitSet
742-
pub fn init() StringBitSet {
743-
return .{ .bits = .{
744-
BackingSet.initEmpty(),
745-
BackingSet.initEmpty(),
746-
BackingSet.initEmpty(),
747-
BackingSet.initEmpty(),
748-
} };
749-
}
750-
751-
/// setValue - sets the value of the bit at the specified position
752-
pub fn setValue(self: *StringBitSet, pos: usize, value: bool) void {
753-
const mod_pos = pos & 63;
754-
switch (pos) {
755-
0...63 => self.bits[0].setValue(mod_pos, value),
756-
64...127 => self.bits[1].setValue(mod_pos, value),
757-
128...191 => self.bits[2].setValue(mod_pos, value),
758-
192...255 => self.bits[3].setValue(mod_pos, value),
759-
else => unreachable,
760-
}
761-
}
762-
763-
/// isSet - checks if the bit at the specified position is set
764-
pub fn isSet(self: *const StringBitSet, pos: usize) bool {
765-
const mod_pos = pos & 63;
766-
return switch (pos) {
767-
0...63 => self.bits[0].isSet(mod_pos),
768-
64...127 => self.bits[1].isSet(mod_pos),
769-
128...191 => self.bits[2].isSet(mod_pos),
770-
192...255 => self.bits[3].isSet(mod_pos),
771-
else => unreachable,
772-
};
773-
}
774-
775-
/// unionWith - computes the union of two StringBitSets
776-
pub fn unionWith(self: StringBitSet, other: StringBitSet) StringBitSet {
777-
return .{ .bits = .{
778-
self.bits[0].unionWith(other.bits[0]),
779-
self.bits[1].unionWith(other.bits[1]),
780-
self.bits[2].unionWith(other.bits[2]),
781-
self.bits[3].unionWith(other.bits[3]),
782-
} };
783-
}
784-
785-
/// differenceWith - computes the difference of two StringBitSets
786-
pub fn differenceWith(self: StringBitSet, other: StringBitSet) StringBitSet {
787-
return .{ .bits = .{
788-
self.bits[0].differenceWith(other.bits[0]),
789-
self.bits[1].differenceWith(other.bits[1]),
790-
self.bits[2].differenceWith(other.bits[2]),
791-
self.bits[3].differenceWith(other.bits[3]),
792-
} };
793-
}
794-
795-
/// intersectWith - computes the intersection of two StringBitSets
796-
pub fn intersectWith(self: StringBitSet, other: StringBitSet) StringBitSet {
797-
return .{ .bits = .{
798-
self.bits[0].intersectWith(other.bits[0]),
799-
self.bits[1].intersectWith(other.bits[1]),
800-
self.bits[2].intersectWith(other.bits[2]),
801-
self.bits[3].intersectWith(other.bits[3]),
802-
} };
803-
}
804-
805-
/// count - counts the number of set bits in the StringBitSet
806-
pub fn count(self: StringBitSet) usize {
807-
return self.bits[0].count() + self.bits[1].count() + self.bits[2].count() + self.bits[3].count();
808-
}
809-
810-
/// fillBuffer - fills a buffer with the values of set bits in the StringBitSet
811-
pub fn fillBuffer(self: *const StringBitSet, buffer: []u8) []u8 {
812-
var val: usize = 0;
813-
var pos: usize = 0;
814-
while (val < 256) : (val += 1) {
815-
if (self.isSet(val)) {
816-
buffer[pos] = @intCast(val);
817-
pos += 1;
818-
}
819-
}
820-
return buffer[0..pos];
821-
}
822-
};
823-
824-
//////////////////////////////////////////////////////////////////////////////////
825-
// ENUMERATED OPTIONS : //
826-
//////////////////////////////////////////////////////////////////////////////////
827-
828-
const DirectionOption = enum {
829-
all,
830-
left,
831-
right,
832-
};
833-
834-
const ReplaceOption = enum {
835-
first,
836-
last,
837-
all,
838-
periphery,
839-
};
840-
841-
const TrimOptions = enum {
842-
scalar,
843-
predicate,
844-
any,
845-
};
846-
847-
const SortDirection = enum {
848-
asc,
849-
desc,
850-
};
851-
852-
const SampleOption = enum {
853-
scalar,
854-
sequence,
855-
};
856-
857-
// any, sequence, scalar
858-
const FluentMode = std.mem.DelimiterType;
859-
860705
////////////////////////////////////////////////////////////////////////////////
861706
// TESTING BLOCK : ///
862707
////////////////////////////////////////////////////////////////////////////////

src/fluent_enum.zig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const std = @import("std");
2+
3+
pub const StringMode = enum {
4+
regex,
5+
scalar,
6+
};
7+
8+
pub const DirectionOption = enum {
9+
all,
10+
left,
11+
right,
12+
};
13+
14+
pub const ReplaceOption = enum {
15+
first,
16+
last,
17+
all,
18+
periphery,
19+
};
20+
21+
pub const TrimOptions = enum {
22+
scalar,
23+
predicate,
24+
any,
25+
regex,
26+
};
27+
28+
pub const SortDirection = enum {
29+
asc,
30+
desc,
31+
};
32+
33+
pub const SampleOption = enum {
34+
scalar,
35+
sequence,
36+
};
37+
38+
// any, sequence, scalar
39+
pub const FluentMode = std.mem.DelimiterType;

src/fluent_static_bitset.zig

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const std = @import("std");
2+
3+
const StringBitSet = struct {
4+
const BackingSet = std.StaticBitSet(@bitSizeOf(usize));
5+
6+
bits: [4]BackingSet,
7+
8+
/// init - returns an initEmpty instance of StringBitSet
9+
pub fn init() StringBitSet {
10+
return .{ .bits = .{
11+
BackingSet.initEmpty(),
12+
BackingSet.initEmpty(),
13+
BackingSet.initEmpty(),
14+
BackingSet.initEmpty(),
15+
} };
16+
}
17+
18+
/// setValue - sets the value of the bit at the specified position
19+
pub fn setValue(self: *StringBitSet, pos: usize, value: bool) void {
20+
const mod_pos = pos & 63;
21+
switch (pos) {
22+
0...63 => self.bits[0].setValue(mod_pos, value),
23+
64...127 => self.bits[1].setValue(mod_pos, value),
24+
128...191 => self.bits[2].setValue(mod_pos, value),
25+
192...255 => self.bits[3].setValue(mod_pos, value),
26+
else => unreachable,
27+
}
28+
}
29+
30+
/// isSet - checks if the bit at the specified position is set
31+
pub fn isSet(self: *const StringBitSet, pos: usize) bool {
32+
const mod_pos = pos & 63;
33+
return switch (pos) {
34+
0...63 => self.bits[0].isSet(mod_pos),
35+
64...127 => self.bits[1].isSet(mod_pos),
36+
128...191 => self.bits[2].isSet(mod_pos),
37+
192...255 => self.bits[3].isSet(mod_pos),
38+
else => unreachable,
39+
};
40+
}
41+
42+
/// unionWith - computes the union of two StringBitSets
43+
pub fn unionWith(self: StringBitSet, other: StringBitSet) StringBitSet {
44+
return .{ .bits = .{
45+
self.bits[0].unionWith(other.bits[0]),
46+
self.bits[1].unionWith(other.bits[1]),
47+
self.bits[2].unionWith(other.bits[2]),
48+
self.bits[3].unionWith(other.bits[3]),
49+
} };
50+
}
51+
52+
/// differenceWith - computes the difference of two StringBitSets
53+
pub fn differenceWith(self: StringBitSet, other: StringBitSet) StringBitSet {
54+
return .{ .bits = .{
55+
self.bits[0].differenceWith(other.bits[0]),
56+
self.bits[1].differenceWith(other.bits[1]),
57+
self.bits[2].differenceWith(other.bits[2]),
58+
self.bits[3].differenceWith(other.bits[3]),
59+
} };
60+
}
61+
62+
/// intersectWith - computes the intersection of two StringBitSets
63+
pub fn intersectWith(self: StringBitSet, other: StringBitSet) StringBitSet {
64+
return .{ .bits = .{
65+
self.bits[0].intersectWith(other.bits[0]),
66+
self.bits[1].intersectWith(other.bits[1]),
67+
self.bits[2].intersectWith(other.bits[2]),
68+
self.bits[3].intersectWith(other.bits[3]),
69+
} };
70+
}
71+
72+
/// count - counts the number of set bits in the StringBitSet
73+
pub fn count(self: StringBitSet) usize {
74+
return self.bits[0].count() + self.bits[1].count() + self.bits[2].count() + self.bits[3].count();
75+
}
76+
77+
/// fillBuffer - fills a buffer with the values of set bits in the StringBitSet
78+
pub fn fillBuffer(self: *const StringBitSet, buffer: []u8) []u8 {
79+
var val: usize = 0;
80+
var pos: usize = 0;
81+
while (val < 256) : (val += 1) {
82+
if (self.isSet(val)) {
83+
buffer[pos] = @intCast(val);
84+
pos += 1;
85+
}
86+
}
87+
return buffer[0..pos];
88+
}
89+
};

0 commit comments

Comments
 (0)