Branch data Line data Source code
1 : : /* 2 : : * Copyright (c) 2015 Andrew Kelley 3 : : * 4 : : * This file is part of zig, which is MIT licensed. 5 : : * See http://opensource.org/licenses/MIT 6 : : */ 7 : : 8 : : #include <stdlib.h> 9 : : #include <stdio.h> 10 : : #include <stdarg.h> 11 : : 12 : : #include "util.hpp" 13 : : #include "userland.h" 14 : : 15 : 0 : void zig_panic(const char *format, ...) { 16 : : va_list ap; 17 : 0 : va_start(ap, format); 18 : 0 : vfprintf(stderr, format, ap); 19 : 0 : fflush(stderr); 20 : 0 : va_end(ap); 21 : 0 : stage2_panic(nullptr, 0); 22 : : abort(); 23 : : } 24 : : 25 : 743081092 : void assert(bool ok) { 26 [ - + ]: 743081092 : if (!ok) { 27 : 0 : const char *msg = "Assertion failed. This is a bug in the Zig compiler."; 28 : 0 : stage2_panic(msg, strlen(msg)); 29 : : } 30 : 743081092 : } 31 : : 32 : 0 : uint32_t int_hash(int i) { 33 : 0 : return (uint32_t)(i % UINT32_MAX); 34 : : } 35 : 0 : bool int_eq(int a, int b) { 36 : 0 : return a == b; 37 : : } 38 : : 39 : 0 : uint32_t uint64_hash(uint64_t i) { 40 : 0 : return (uint32_t)(i % UINT32_MAX); 41 : : } 42 : : 43 : 0 : bool uint64_eq(uint64_t a, uint64_t b) { 44 : 0 : return a == b; 45 : : } 46 : : 47 : 0 : uint32_t ptr_hash(const void *ptr) { 48 : 0 : return (uint32_t)(((uintptr_t)ptr) % UINT32_MAX); 49 : : } 50 : : 51 : 0 : bool ptr_eq(const void *a, const void *b) { 52 : 0 : return a == b; 53 : : } 54 : : 55 : : // Ported from std/mem.zig. 56 : 2253647 : bool SplitIterator_isSplitByte(SplitIterator *self, uint8_t byte) { 57 [ + + ]: 4076911 : for (size_t i = 0; i < self->split_bytes.len; i += 1) { 58 [ + + ]: 2259611 : if (byte == self->split_bytes.ptr[i]) { 59 : 436347 : return true; 60 : : } 61 : : } 62 : 1817300 : return false; 63 : : } 64 : : 65 : : // Ported from std/mem.zig. 66 : 339137 : Optional<Slice<uint8_t>> SplitIterator_next(SplitIterator *self) { 67 : : // move to beginning of token 68 [ + + + + ]: 776501 : while (self->index < self->buffer.len && [ + + ] 69 : 327985 : SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) 70 : : { 71 : 109379 : self->index += 1; 72 : : } 73 : 339137 : size_t start = self->index; 74 [ + + ]: 339137 : if (start == self->buffer.len) { 75 : 120531 : return {}; 76 : : } 77 : : 78 : : // move to end of token 79 [ + + + + ]: 1861468 : while (self->index < self->buffer.len && [ + + ] 80 : 1526482 : !SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) 81 : : { 82 : 1424256 : self->index += 1; 83 : : } 84 : 218606 : size_t end = self->index; 85 : : 86 : 218606 : return Optional<Slice<uint8_t>>::some(self->buffer.slice(start, end)); 87 : : } 88 : : 89 : : // Ported from std/mem.zig. 90 : : // This one won't collapse multiple separators into one, so you could use it, for example, 91 : : // to parse Comma Separated Value format. 92 : 111122 : Optional<Slice<uint8_t>> SplitIterator_next_separate(SplitIterator *self) { 93 : : // move to beginning of token 94 [ + - + + ]: 222244 : if (self->index < self->buffer.len && [ + + ] 95 : 111122 : SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) 96 : : { 97 : 111120 : self->index += 1; 98 : : } 99 : 111122 : size_t start = self->index; 100 [ + + ]: 111122 : if (start == self->buffer.len) { 101 : 2 : return {}; 102 : : } 103 : : 104 : : // move to end of token 105 [ + - + + ]: 394174 : while (self->index < self->buffer.len && [ + + ] 106 : 283054 : !SplitIterator_isSplitByte(self, self->buffer.ptr[self->index])) 107 : : { 108 : 171934 : self->index += 1; 109 : : } 110 : 111120 : size_t end = self->index; 111 : : 112 : 111120 : return Optional<Slice<uint8_t>>::some(self->buffer.slice(start, end)); 113 : : } 114 : : 115 : : // Ported from std/mem.zig 116 : 2502 : Slice<uint8_t> SplitIterator_rest(SplitIterator *self) { 117 : : // move to beginning of token 118 : 2502 : size_t index = self->index; 119 [ + - ][ + + ]: 5004 : while (index < self->buffer.len && SplitIterator_isSplitByte(self, self->buffer.ptr[index])) { [ + + ] 120 : 2502 : index += 1; 121 : : } 122 : 2502 : return self->buffer.sliceFrom(index); 123 : : } 124 : : 125 : : // Ported from std/mem.zig 126 : 130523 : SplitIterator memSplit(Slice<uint8_t> buffer, Slice<uint8_t> split_bytes) { 127 : 130523 : return SplitIterator{0, buffer, split_bytes}; 128 : : }