[f450f2f] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // string_res -- variable-length, mutable run of text, with resource semantics |
---|
| 8 | // |
---|
| 9 | // Author : Michael L. Brooks |
---|
| 10 | // Created On : Fri Sep 03 11:00:00 2021 |
---|
| 11 | // Last Modified By : Michael L. Brooks |
---|
| 12 | // Last Modified On : Fri Sep 03 11:00:00 2021 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #pragma once |
---|
| 17 | |
---|
| 18 | #include <fstream.hfa> |
---|
[4e8df745] | 19 | #include <string.h> // e.g. strlen |
---|
[f450f2f] | 20 | |
---|
| 21 | |
---|
| 22 | //######################### HandleNode ######################### |
---|
| 23 | //private |
---|
| 24 | |
---|
| 25 | struct VbyteHeap; |
---|
| 26 | |
---|
| 27 | struct HandleNode { |
---|
| 28 | HandleNode *flink; // forward link |
---|
| 29 | HandleNode *blink; // backward link |
---|
[0f781fb8] | 30 | VbyteHeap *ulink; // upward link |
---|
[f450f2f] | 31 | |
---|
| 32 | char *s; // pointer to byte string |
---|
| 33 | unsigned int lnth; // length of byte string |
---|
| 34 | }; // HandleNode |
---|
| 35 | |
---|
[0f781fb8] | 36 | VbyteHeap * DEBUG_string_heap(); |
---|
[7b0e8b7] | 37 | size_t DEBUG_string_bytes_in_heap( VbyteHeap * heap ); |
---|
[6cc87c0] | 38 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ); |
---|
| 39 | const char * DEBUG_string_heap_start( VbyteHeap * heap ); |
---|
| 40 | |
---|
[08ed947] | 41 | void TUNING_set_string_heap_liveness_threshold( double val ); |
---|
[f450f2f] | 42 | |
---|
| 43 | //######################### String ######################### |
---|
| 44 | |
---|
| 45 | // A dynamically-sized string |
---|
| 46 | struct string_res { |
---|
| 47 | HandleNode Handle; // chars, start, end, global neighbours |
---|
[804bf677] | 48 | bool shareEditSet_owns_ulink; |
---|
[f450f2f] | 49 | string_res * shareEditSet_prev; |
---|
| 50 | string_res * shareEditSet_next; |
---|
| 51 | }; |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | //######################### charclass_res ######################### |
---|
| 55 | |
---|
| 56 | struct charclass_res { |
---|
| 57 | string_res chars; |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | void ?{}( charclass_res & ) = void; |
---|
| 61 | void ?{}( charclass_res &, charclass_res) = void; |
---|
| 62 | charclass_res ?=?( charclass_res &, charclass_res) = void; |
---|
| 63 | void ?{}( charclass_res &, const string_res & chars); |
---|
| 64 | void ?{}( charclass_res &, const char * chars ); |
---|
| 65 | void ?{}( charclass_res &, const char * chars, size_t charssize ); |
---|
| 66 | void ^?{}( charclass_res & ); |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | //######################### String ######################### |
---|
| 70 | |
---|
| 71 | // Getters |
---|
| 72 | size_t size(const string_res &s); |
---|
| 73 | |
---|
| 74 | // Constructors, Assignment Operators, Destructor |
---|
| 75 | void ?{}(string_res &s); // empty string |
---|
| 76 | void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
[4e8df745] | 77 | static inline void ?{}(string_res &s, const char* rhs) { // copy from string literal (NULL-terminated) |
---|
| 78 | (s){ rhs, strlen(rhs) }; |
---|
| 79 | } |
---|
[f450f2f] | 80 | |
---|
| 81 | void ?{}(string_res &s, const string_res & s2) = void; |
---|
| 82 | void ?{}(string_res &s, string_res & s2) = void; |
---|
| 83 | |
---|
| 84 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS }; |
---|
| 85 | void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end ); |
---|
| 86 | static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) { |
---|
| 87 | ?{}( s, src, mode, 0, size(src)); |
---|
| 88 | } |
---|
| 89 | |
---|
[1733184] | 90 | string_res & assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
| 91 | static inline string_res & ?=?(string_res &s, const char* other) { // copy from string literal (NULL-terminated) |
---|
| 92 | return assign(s, other, strlen(other)); |
---|
[4e8df745] | 93 | } |
---|
[1733184] | 94 | string_res & ?=?(string_res &s, const string_res &other); |
---|
| 95 | string_res & ?=?(string_res &s, string_res &other); |
---|
| 96 | string_res & ?=?(string_res &s, char other); |
---|
[f450f2f] | 97 | |
---|
| 98 | void ^?{}(string_res &s); |
---|
| 99 | |
---|
| 100 | // IO Operator |
---|
| 101 | ofstream & ?|?(ofstream &out, const string_res &s); |
---|
| 102 | void ?|?(ofstream &out, const string_res &s); |
---|
| 103 | |
---|
| 104 | // Concatenation |
---|
[4e8df745] | 105 | void append(string_res &s, const char* buffer, size_t bsize); |
---|
[f450f2f] | 106 | void ?+=?(string_res &s, char other); // append a character |
---|
| 107 | void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string |
---|
[4e8df745] | 108 | static inline void ?+=?(string_res &s, const char* other) { |
---|
| 109 | append( s, other, strlen(other) ); |
---|
| 110 | } |
---|
[f450f2f] | 111 | |
---|
| 112 | // Character access |
---|
[218096f] | 113 | void assignAt(const string_res &s, size_t index, char val); |
---|
[f450f2f] | 114 | char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's |
---|
| 115 | //char codePointAt(const string_res &s, size_t index); // revisit under Unicode |
---|
| 116 | |
---|
| 117 | // Comparisons |
---|
| 118 | bool ?==?(const string_res &s, const string_res &other); |
---|
| 119 | bool ?!=?(const string_res &s, const string_res &other); |
---|
| 120 | bool ?==?(const string_res &s, const char* other); |
---|
| 121 | bool ?!=?(const string_res &s, const char* other); |
---|
| 122 | |
---|
| 123 | // String search |
---|
| 124 | bool contains(const string_res &s, char ch); // single character |
---|
| 125 | |
---|
| 126 | int find(const string_res &s, char search); |
---|
| 127 | int find(const string_res &s, const string_res &search); |
---|
| 128 | int find(const string_res &s, const char* search); |
---|
| 129 | int find(const string_res &s, const char* search, size_t searchsize); |
---|
| 130 | |
---|
[08ed947] | 131 | int findFrom(const string_res &s, size_t fromPos, char search); |
---|
| 132 | int findFrom(const string_res &s, size_t fromPos, const string_res &search); |
---|
| 133 | int findFrom(const string_res &s, size_t fromPos, const char* search); |
---|
| 134 | int findFrom(const string_res &s, size_t fromPos, const char* search, size_t searchsize); |
---|
| 135 | |
---|
[f450f2f] | 136 | bool includes(const string_res &s, const string_res &search); |
---|
| 137 | bool includes(const string_res &s, const char* search); |
---|
| 138 | bool includes(const string_res &s, const char* search, size_t searchsize); |
---|
| 139 | |
---|
| 140 | bool startsWith(const string_res &s, const string_res &prefix); |
---|
| 141 | bool startsWith(const string_res &s, const char* prefix); |
---|
| 142 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize); |
---|
| 143 | |
---|
| 144 | bool endsWith(const string_res &s, const string_res &suffix); |
---|
| 145 | bool endsWith(const string_res &s, const char* suffix); |
---|
| 146 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize); |
---|
| 147 | |
---|
| 148 | int include(const string_res &s, const charclass_res &mask); |
---|
| 149 | int exclude(const string_res &s, const charclass_res &mask); |
---|
| 150 | |
---|
| 151 | // Modifiers |
---|
| 152 | void padStart(string_res &s, size_t n); |
---|
| 153 | void padStart(string_res &s, size_t n, char padding); |
---|
| 154 | void padEnd(string_res &s, size_t n); |
---|
| 155 | void padEnd(string_res &s, size_t n, char padding); |
---|
| 156 | |
---|