[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> |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | //######################### HandleNode ######################### |
---|
| 22 | //private |
---|
| 23 | |
---|
| 24 | struct VbyteHeap; |
---|
| 25 | |
---|
| 26 | struct HandleNode { |
---|
| 27 | HandleNode *flink; // forward link |
---|
| 28 | HandleNode *blink; // backward link |
---|
| 29 | |
---|
| 30 | char *s; // pointer to byte string |
---|
| 31 | unsigned int lnth; // length of byte string |
---|
| 32 | }; // HandleNode |
---|
| 33 | |
---|
| 34 | void ?{}( HandleNode & ); // constructor for header node |
---|
| 35 | |
---|
| 36 | void ?{}( HandleNode &, VbyteHeap & ); // constructor for nodes in the handle list |
---|
| 37 | void ^?{}( HandleNode & ); // destructor for handle nodes |
---|
| 38 | |
---|
[6cc87c0] | 39 | extern VbyteHeap * DEBUG_string_heap; |
---|
| 40 | size_t DEBUG_string_bytes_avail_until_gc( VbyteHeap * heap ); |
---|
| 41 | const char * DEBUG_string_heap_start( VbyteHeap * heap ); |
---|
| 42 | |
---|
[f450f2f] | 43 | |
---|
| 44 | //######################### String ######################### |
---|
| 45 | |
---|
| 46 | // A dynamically-sized string |
---|
| 47 | struct string_res { |
---|
| 48 | HandleNode Handle; // chars, start, end, global neighbours |
---|
| 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* initial); // copy from string literal (NULL-terminated) |
---|
| 77 | void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
| 78 | |
---|
| 79 | void ?{}(string_res &s, const string_res & s2) = void; |
---|
| 80 | void ?{}(string_res &s, string_res & s2) = void; |
---|
| 81 | |
---|
| 82 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS }; |
---|
| 83 | void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end ); |
---|
| 84 | static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) { |
---|
| 85 | ?{}( s, src, mode, 0, size(src)); |
---|
| 86 | } |
---|
| 87 | |
---|
[d8d512e] | 88 | void assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
| 89 | void ?=?(string_res &s, const char* other); // copy from string literal (NULL-terminated) |
---|
[f450f2f] | 90 | void ?=?(string_res &s, const string_res &other); |
---|
| 91 | void ?=?(string_res &s, string_res &other); |
---|
[d8d512e] | 92 | void ?=?(string_res &s, char other); |
---|
[f450f2f] | 93 | |
---|
| 94 | void ^?{}(string_res &s); |
---|
| 95 | |
---|
| 96 | // IO Operator |
---|
| 97 | ofstream & ?|?(ofstream &out, const string_res &s); |
---|
| 98 | void ?|?(ofstream &out, const string_res &s); |
---|
| 99 | |
---|
| 100 | // Concatenation |
---|
| 101 | void ?+=?(string_res &s, char other); // append a character |
---|
| 102 | void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string |
---|
[d8d512e] | 103 | void ?+=?(string_res &s, const char* other); |
---|
| 104 | void append(string_res &s, const char* buffer, size_t bsize); |
---|
[f450f2f] | 105 | |
---|
| 106 | // Character access |
---|
| 107 | char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's |
---|
| 108 | //char codePointAt(const string_res &s, size_t index); // revisit under Unicode |
---|
| 109 | |
---|
| 110 | // Comparisons |
---|
| 111 | bool ?==?(const string_res &s, const string_res &other); |
---|
| 112 | bool ?!=?(const string_res &s, const string_res &other); |
---|
| 113 | bool ?==?(const string_res &s, const char* other); |
---|
| 114 | bool ?!=?(const string_res &s, const char* other); |
---|
| 115 | |
---|
| 116 | // String search |
---|
| 117 | bool contains(const string_res &s, char ch); // single character |
---|
| 118 | |
---|
| 119 | int find(const string_res &s, char search); |
---|
| 120 | int find(const string_res &s, const string_res &search); |
---|
| 121 | int find(const string_res &s, const char* search); |
---|
| 122 | int find(const string_res &s, const char* search, size_t searchsize); |
---|
| 123 | |
---|
| 124 | bool includes(const string_res &s, const string_res &search); |
---|
| 125 | bool includes(const string_res &s, const char* search); |
---|
| 126 | bool includes(const string_res &s, const char* search, size_t searchsize); |
---|
| 127 | |
---|
| 128 | bool startsWith(const string_res &s, const string_res &prefix); |
---|
| 129 | bool startsWith(const string_res &s, const char* prefix); |
---|
| 130 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize); |
---|
| 131 | |
---|
| 132 | bool endsWith(const string_res &s, const string_res &suffix); |
---|
| 133 | bool endsWith(const string_res &s, const char* suffix); |
---|
| 134 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize); |
---|
| 135 | |
---|
| 136 | int include(const string_res &s, const charclass_res &mask); |
---|
| 137 | int exclude(const string_res &s, const charclass_res &mask); |
---|
| 138 | |
---|
| 139 | // Modifiers |
---|
| 140 | void padStart(string_res &s, size_t n); |
---|
| 141 | void padStart(string_res &s, size_t n, char padding); |
---|
| 142 | void padEnd(string_res &s, size_t n); |
---|
| 143 | void padEnd(string_res &s, size_t n, char padding); |
---|
| 144 | |
---|