aboutsummaryrefslogtreecommitdiff
path: root/src/string_builder.zig
blob: d46b014e236684b72453d61c4894ef0d7975d7e4 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
const string = @import("string_types.zig").string;
const Allocator = @import("std").mem.Allocator;
const assert = @import("std").debug.assert;
const copy = @import("std").mem.copy;
const Env = @import("./env.zig");
const bun = @import("./global.zig");
const StringBuilder = @This();
const DebugHashTable = if (Env.allow_assert) std.AutoHashMapUnmanaged(u64, void) else void;

len: usize = 0,
cap: usize = 0,
ptr: ?[*]u8 = null,

debug_only_checker: DebugHashTable = DebugHashTable{},

pub fn count(this: *StringBuilder, slice: string) void {
    this.cap += slice.len;
    if (comptime Env.allow_assert) {
        _ = this.debug_only_checker.getOrPut(bun.default_allocator, bun.hash(slice)) catch unreachable;
    }
}

pub fn allocate(this: *StringBuilder, allocator: Allocator) !void {
    var slice = try allocator.alloc(u8, this.cap);
    this.ptr = slice.ptr;
    this.len = 0;
}

pub fn deinit(this: *StringBuilder, allocator: Allocator) void {
    if (this.ptr == null or this.cap == 0) return;
    allocator.free(this.ptr.?[0..this.cap]);
    if (comptime Env.allow_assert) {
        this.debug_only_checker.deinit(bun.default_allocator);
        this.debug_only_checker = .{};
    }
}

pub fn append(this: *StringBuilder, slice: string) string {
    if (comptime Env.allow_assert) {
        assert(this.len <= this.cap); // didn't count everything
        assert(this.ptr != null); // must call allocate first
    }

    if (comptime Env.allow_assert) {
        assert(this.debug_only_checker.contains(bun.hash(slice)));
    }

    bun.copy(u8, this.ptr.?[this.len..this.cap], slice);
    const result = this.ptr.?[this.len..this.cap][0..slice.len];
    this.len += slice.len;

    if (Env.allow_assert) {
        assert(this.len <= this.cap);
    }

    return result;
}

const std = @import("std");
pub fn fmt(this: *StringBuilder, comptime str: string, args: anytype) string {
    if (Env.allow_assert) {
        assert(this.len <= this.cap); // didn't count everything
        assert(this.ptr != null); // must call allocate first
    }

    var buf = this.ptr.?[this.len..this.cap];
    const out = std.fmt.bufPrint(buf, str, args) catch unreachable;
    this.len += out.len;

    if (Env.allow_assert) {
        assert(this.len <= this.cap);
    }

    return out;
}

pub fn fmtCount(this: *StringBuilder, comptime str: string, args: anytype) void {
    this.cap += std.fmt.count(str, args);
}
/vercel@3.7.2&id=8ee94ffbd9b0c055a245f63e766f64f51466eda7&follow=1'>Remove weird changeset symbols (#7648)Gravatar Bjorn Lu 2-2/+2 2023-07-14Refactor merge server config (#7639)Gravatar Bjorn Lu 3-28/+26 2023-07-13[ci] formatGravatar matthewp 4-16/+19 2023-07-13Fixes for redirects config (#7644)Gravatar Matthew Phillips 9-14/+110 2023-07-13fix(assets): Fix images not following EXIF rotation (#7637)Gravatar Erika 7-8/+46 2023-07-13[ci] formatGravatar Princesseuh 1-1/+1 2023-07-13Allow SVGs when using Assets (#7643)Gravatar Alvin Bryan 8-17/+46 2023-07-13Convert CLI top-level imports as dynamic imports (#7635)Gravatar Bjorn Lu 4-46/+45 2023-07-13[ci] release (#7624)astro@2.8.2@astrojs/vercel@3.7.1@astrojs/underscore-redirects@0.2.0@astrojs/netlify@2.4.0@astrojs/cloudflare@6.6.1Gravatar Houston (Bot) 49-108/+129 2023-07-13[ci] formatGravatar sarah11918 1-5/+10 2023-07-13[docs] update image integration README (#7633)Gravatar Sarah Rainsberger 1-2/+69 2023-07-13[ci] formatGravatar bluwy 1-1/+1 2023-07-13Move all CLI commands to their subfolders (#7634)Gravatar Bjorn Lu 7-102/+170 2023-07-13[ci] formatGravatar ematipico 1-2/+2 2023-07-13feat(@astrojs/netlify): add `build.split` support (#7615)Gravatar Emanuele Stoppa 20-35/+234 2023-07-13Update turbo cache flow (#7631)Gravatar Bjorn Lu 1-33/+36 2023-07-12Fix `build.excludeMiddleware` default type docs (#7630)Gravatar Yan Thomas 1-1/+1 2023-07-12Refactor config loading (#7622)Gravatar Bjorn Lu 6-151/+90 2023-07-12[ci] formatGravatar bluwy 1-1/+1 2023-07-12feat: bumped typescript-eslint to v6 stable 🥳 (#7626)Gravatar Josh Goldberg ✨ 4-43/+49 2023-07-12fix(@astrojs/vercel): improve file detection (#7621)Gravatar Emanuele Stoppa 16-25/+113 2023-07-11[ci] formatGravatar matthewp 1-1/+1 2023-07-11Fixes Response not being cloneable by middleware (#7623)Gravatar Matthew Phillips 5-0/+37 2023-07-11[ci] release (#7594)astro@2.8.1@astrojs/prefetch@0.3.0@astrojs/markdoc@0.4.2Gravatar Houston (Bot) 50-104/+100 2023-07-11Filter out astro from `peerDependencies` in `astro add` (#7620)Gravatar Chris Swithinbank 2-1/+7 2023-07-11[ci] formatGravatar bluwy 1-1/+1 2023-07-11Refactor simple CLI commands (#7619)Gravatar Bjorn Lu 12-115/+123 2023-07-10fix: `astro info` command fallback for package manager (#7618)Gravatar Emanuele Stoppa 2-1/+7 2023-07-10Add CLI startup speed benchmark (#7617)Gravatar Bjorn Lu 5-11/+95 2023-07-10[ci] formatGravatar bluwy 1-1/+1