Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.61 KB | None | 0 0
  1. const std = @import("std");
  2. const mem = std.mem;
  3. const Allocator = std.mem.Allocator;
  4.  
  5. const String = struct {
  6.     const Self = @This();
  7.  
  8.     buffer: []u8,
  9.     idx: usize,
  10.     allocator: *Allocator,
  11.  
  12.     /// Creates a new String.
  13.     /// This function does NOT allocate.
  14.     pub fn new(alloc: *Allocator) Self {
  15.         return Self {
  16.             .buffer = [0]u8 {},
  17.             .idx = 0,
  18.             .allocator = alloc
  19.         };
  20.     }
  21.    
  22.     /// Creates a new String from a String-slice.
  23.     /// This allocates enough space to hold the contents of `str`, and then copies the contents.
  24.     pub fn From(alloc: *Allocator, str: []const u8) !Self {
  25.         var s = Self.new(alloc);
  26.         try s.extend(str);
  27.         return s;
  28.     }
  29.  
  30.     /// Creates a new String that can hold `size` items without reallocating.
  31.     /// This function allocates enough space to hold `size` items.
  32.     pub fn with_capacity(alloc: *Allocator, size: usize) !Self {
  33.         var s = Self.new(alloc);
  34.         try s.reserve(size);
  35.  
  36.         return s;
  37.     }
  38.    
  39.     /// Free's the String.
  40.     /// This should be called after creating a String.
  41.     pub fn deinit(self: *Self) void {
  42.         self.allocator.free(self.buffer);
  43.     }
  44.  
  45.     /// Returns a view into the String's buffer.
  46.     pub fn asStr(self: *Self) []const u8 {
  47.         return self.buffer[0..self.idx];
  48.     }
  49.  
  50.     /// Extends the String with the contents of `str`.
  51.     /// If the String has not enough allocated space to hold the contents of `str`, this function will allocate.
  52.     pub fn extend(self: *Self, str: []const u8) !void {
  53.         try self.reserve(str.len);
  54.         mem.copy(u8, self.buffer[self.idx..], str);
  55.         self.idx += str.len;
  56.     }
  57.  
  58.     pub fn push(self: *Self, byte: u8) !void {
  59.         try self.reserve(1);
  60.         self.buffer[self.idx] = byte;
  61.         self.idx += 1;
  62.     }
  63.     /// Reserves `size` elements in the buffer.
  64.     pub fn reserve(self: *Self, size: usize) !void {
  65.         if (self.idx + size > self.cap()) {
  66.             var new_buffer = try self.allocator.realloc(u8, self.buffer, self.idx + size);
  67.             self.buffer = new_buffer;
  68.         }
  69.     }
  70.  
  71.     /// Returns the capacity of the String. This is how many items the String can hold untill it is full.
  72.     pub fn cap(self: *Self) usize {
  73.         return self.buffer.len;
  74.     }
  75. };
  76.  
  77.  
  78. pub fn main() !void {
  79.     var da = std.heap.DirectAllocator.init();
  80.  
  81.     const alloc = &da.allocator;
  82.    
  83.     var s = try String.with_capacity(alloc, 12);
  84.     defer s.deinit();
  85.     try s.extend("Hello sailor");
  86.     try s.push('!');
  87.  
  88.     std.debug.warn("{}\n", s.asStr());
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement