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 | |
---|
39 | |
---|
40 | //######################### String ######################### |
---|
41 | |
---|
42 | // A dynamically-sized string |
---|
43 | struct string_res { |
---|
44 | HandleNode Handle; // chars, start, end, global neighbours |
---|
45 | string_res * shareEditSet_prev; |
---|
46 | string_res * shareEditSet_next; |
---|
47 | }; |
---|
48 | |
---|
49 | |
---|
50 | //######################### charclass_res ######################### |
---|
51 | |
---|
52 | struct charclass_res { |
---|
53 | string_res chars; |
---|
54 | }; |
---|
55 | |
---|
56 | void ?{}( charclass_res & ) = void; |
---|
57 | void ?{}( charclass_res &, charclass_res) = void; |
---|
58 | charclass_res ?=?( charclass_res &, charclass_res) = void; |
---|
59 | void ?{}( charclass_res &, const string_res & chars); |
---|
60 | void ?{}( charclass_res &, const char * chars ); |
---|
61 | void ?{}( charclass_res &, const char * chars, size_t charssize ); |
---|
62 | void ^?{}( charclass_res & ); |
---|
63 | |
---|
64 | |
---|
65 | //######################### String ######################### |
---|
66 | |
---|
67 | // Getters |
---|
68 | size_t size(const string_res &s); |
---|
69 | |
---|
70 | // Constructors, Assignment Operators, Destructor |
---|
71 | void ?{}(string_res &s); // empty string |
---|
72 | void ?{}(string_res &s, const char* initial); // copy from string literal (NULL-terminated) |
---|
73 | void ?{}(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
74 | |
---|
75 | void ?{}(string_res &s, const string_res & s2) = void; |
---|
76 | void ?{}(string_res &s, string_res & s2) = void; |
---|
77 | |
---|
78 | enum StrResInitMode { COPY_VALUE, SHARE_EDITS }; |
---|
79 | void ?{}(string_res &s, const string_res & src, StrResInitMode, size_t start, size_t end ); |
---|
80 | static inline void ?{}(string_res &s, const string_res & src, StrResInitMode mode ) { |
---|
81 | ?{}( s, src, mode, 0, size(src)); |
---|
82 | } |
---|
83 | |
---|
84 | void assign(string_res &s, const char* buffer, size_t bsize); // copy specific length from buffer |
---|
85 | void ?=?(string_res &s, const char* other); // copy from string literal (NULL-terminated) |
---|
86 | void ?=?(string_res &s, const string_res &other); |
---|
87 | void ?=?(string_res &s, string_res &other); |
---|
88 | void ?=?(string_res &s, char other); |
---|
89 | |
---|
90 | void ^?{}(string_res &s); |
---|
91 | |
---|
92 | // IO Operator |
---|
93 | ofstream & ?|?(ofstream &out, const string_res &s); |
---|
94 | void ?|?(ofstream &out, const string_res &s); |
---|
95 | |
---|
96 | // Concatenation |
---|
97 | void ?+=?(string_res &s, char other); // append a character |
---|
98 | void ?+=?(string_res &s, const string_res &s2); // append-concatenate to first string |
---|
99 | void ?+=?(string_res &s, const char* other); |
---|
100 | void append(string_res &s, const char* buffer, size_t bsize); |
---|
101 | |
---|
102 | // Character access |
---|
103 | char ?[?](const string_res &s, size_t index); // Mike changed to ret by val from Sunjay's ref, to match Peter's |
---|
104 | //char codePointAt(const string_res &s, size_t index); // revisit under Unicode |
---|
105 | |
---|
106 | // Comparisons |
---|
107 | bool ?==?(const string_res &s, const string_res &other); |
---|
108 | bool ?!=?(const string_res &s, const string_res &other); |
---|
109 | bool ?==?(const string_res &s, const char* other); |
---|
110 | bool ?!=?(const string_res &s, const char* other); |
---|
111 | |
---|
112 | // String search |
---|
113 | bool contains(const string_res &s, char ch); // single character |
---|
114 | |
---|
115 | int find(const string_res &s, char search); |
---|
116 | int find(const string_res &s, const string_res &search); |
---|
117 | int find(const string_res &s, const char* search); |
---|
118 | int find(const string_res &s, const char* search, size_t searchsize); |
---|
119 | |
---|
120 | bool includes(const string_res &s, const string_res &search); |
---|
121 | bool includes(const string_res &s, const char* search); |
---|
122 | bool includes(const string_res &s, const char* search, size_t searchsize); |
---|
123 | |
---|
124 | bool startsWith(const string_res &s, const string_res &prefix); |
---|
125 | bool startsWith(const string_res &s, const char* prefix); |
---|
126 | bool startsWith(const string_res &s, const char* prefix, size_t prefixsize); |
---|
127 | |
---|
128 | bool endsWith(const string_res &s, const string_res &suffix); |
---|
129 | bool endsWith(const string_res &s, const char* suffix); |
---|
130 | bool endsWith(const string_res &s, const char* suffix, size_t suffixsize); |
---|
131 | |
---|
132 | int include(const string_res &s, const charclass_res &mask); |
---|
133 | int exclude(const string_res &s, const charclass_res &mask); |
---|
134 | |
---|
135 | // Modifiers |
---|
136 | void padStart(string_res &s, size_t n); |
---|
137 | void padStart(string_res &s, size_t n, char padding); |
---|
138 | void padEnd(string_res &s, size_t n); |
---|
139 | void padEnd(string_res &s, size_t n, char padding); |
---|
140 | |
---|