source: libcfa/src/collections/string.cfa @ 906d8fa

Last change on this file since 906d8fa was 906d8fa, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

tidy a string-library unhelpful comment

  • Property mode set to 100644
File size: 10.4 KB
RevLine 
[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 -- variable-length, mutable run of text, with value semantics
8//
9// Author           : Michael L. Brooks
10// Created On       : Fri Sep 03 11:00:00 2021
[bc9f84a]11// Last Modified By : Peter A. Buhr
[479fbe3]12// Last Modified On : Sun Jan 14 12:03:47 2024
13// Update Count     : 240
[f450f2f]14//
15
16#include "string.hfa"
17#include "string_res.hfa"
18#include <stdlib.hfa>
19
[accc9df9]20#pragma GCC visibility push(default)
[f450f2f]21
22/*
23Implementation Principle: typical operation translates to the equivalent
24operation on `inner`.  Exceptions are implementing new RAII pattern for value
25semantics and some const-hell handling.
26*/
27
28////////////////////////////////////////////////////////
29// string RAII
30
31// private (not in header)
[681e12f]32static void ?{}( string & s, string_res & src, size_t start, size_t end ) {
33    (s.inner) { malloc() };
34    ?{}( *s.inner, src, SHARE_EDITS, start, end );
[f450f2f]35}
36
[479fbe3]37void ?{}( string & s ) {
38    (s.inner) { malloc() };
39    ?{}( *s.inner );
40}
41
[681e12f]42void ?{}( string & s, const string & c ) {
43    (s.inner) { malloc() };
44    ?{}( *s.inner, *c.inner, COPY_VALUE );
[f450f2f]45}
46
[7abc3de]47void ?{}( string & s, const string & s2, size_t maxlen) {
48    (s.inner) { malloc() };
49    ?{}( *s.inner, *s2.inner, COPY_VALUE, maxlen );
50}
51
52
[681e12f]53void ?{}( string & s, string & c ) {
54    ?{}( s, (const string &) c );
[f450f2f]55}
56
[479fbe3]57void ?{}( string & s, const char c ) {
[681e12f]58    (s.inner) { malloc() };
[7abc3de]59    ?{}( *s.inner, c );
[f450f2f]60}
61
[479fbe3]62void ?{}( string & s, const char * c ) {
[681e12f]63    (s.inner) { malloc() };
[479fbe3]64    ?{}( *s.inner, c );
65}
66
67void ?{}( string & s, const char * c, size_t size) {
68    (s.inner) { malloc() };
69    ?{}( *s.inner, c, size );
[f450f2f]70}
71
[681e12f]72void ^?{}( string & s ) {
73    ^(*s.inner){};
74    free( s.inner );
75    s.inner = 0p;
[f450f2f]76}
77
78////////////////////////////////////////////////////////
79// Alternate construction: request shared edits
80
[681e12f]81string_WithSharedEdits ?`shareEdits( string & s ) {
82    string_WithSharedEdits ret = { &s };
[f450f2f]83    return ret;
84}
85
[681e12f]86void ?{}( string & s, string_WithSharedEdits src ) {
87    ?{}( s, *src.s->inner, 0, src.s->inner->Handle.lnth);
[f450f2f]88}
89
90////////////////////////////////////////////////////////
91// Assignment
92
[681e12f]93void ?=?( string & s, const char * val ) {
94    (*s.inner) = val;
[f450f2f]95}
96
[906d8fa]97// with and without const on "other" argument helps keep prevent autogen ?=? calls
[681e12f]98void ?=?(string & s, const string & c) {
99    (*s.inner) = (*c.inner);
[f450f2f]100}
[906d8fa]101string & ?=?(string & s, string & c) {
102    (*s.inner) = (*c.inner);
103    return s;
104}
[f450f2f]105
[681e12f]106void ?=?( string & s, char val ) {
107    (*s.inner) = val;
[f450f2f]108}
109
[e891349]110void assign(string & s, const string & c, size_t n) {
111    assign(*s.inner, *c.inner, n);
112}
113void assign(string & s, const char * c, size_t n) {
114    assign(*s.inner, c, n);
115}
116
[f450f2f]117
118////////////////////////////////////////////////////////
[d32679d5]119// Input-Output
[f450f2f]120
[681e12f]121ofstream & ?|?( ofstream & out, const string & s ) {
122    return out | (*s.inner); // print internal string_res
[f450f2f]123}
124
[681e12f]125void ?|?( ofstream & out, const string & s ) {
126    (ofstream &)(out | (*s.inner)); ends( out );
[f450f2f]127}
128
[34c6e1e6]129ofstream & ?|?( ofstream & os, _Ostream_Manip(string) f ) {
130        size_t len = size( f.val );
131        char cstr[len + 1];                                                                     // room for null terminator
132        for ( i; len ) cstr[i] = f.val[i];                                      // copy string
133        cstr[len] = '\0';                                                                       // terminate
134        _Ostream_Manip(const char *) cf @= { cstr, f.wd, f.pc, f.base, {f.all} };
135        os | cf | nonl;
136        return os;
137} // ?|?
138
139void ?|?( ofstream & os, _Ostream_Manip(string) f ) {
140        (ofstream &)(os | f); ends( os );
141}
142
[681e12f]143ifstream & ?|?(ifstream & in, string & s) {
144    return in | (*s.inner); // read to internal string_res
[d32679d5]145}
146
[681e12f]147void ?|?( ifstream & in, string & s ) {
148    in | (*s.inner);
[6264087]149}
[d32679d5]150
[34c6e1e6]151ifstream & ?|?( ifstream & is, _Istream_Sstr f ) {
[737988b]152        _Istream_Rstr f2 = {f.s.inner, (_Istream_str_base)f};
153    return is | f2;
[7e1dbd7]154} // ?|?
155
[34c6e1e6]156void ?|?( ifstream & in, _Istream_Sstr f ) {
[f842032]157    (ifstream &)(in | f);
[7e1dbd7]158}
159
[f450f2f]160////////////////////////////////////////////////////////
161// Slicing
162
[681e12f]163string ?()( string & s, size_t start, size_t end ) {
164    string ret = { *s.inner, start, end };
[f450f2f]165    return ret`shareEdits;
166}
167
[681e12f]168string ?()( string & s, size_t start ) {
169    string ret = { *s.inner, start, size( s ) };
[bc9f84a]170    return ret`shareEdits;
171}
172
[f450f2f]173////////////////////////////////////////////////////////
174// Comparison
175
[681e12f]176int  strcmp(const string & s1, const string & s2) { return strcmp(*s1.inner, *s2.inner); }
177bool ?==?(const string & s1, const string & s2) { return *s1.inner == *s2.inner; }
178bool ?!=?(const string & s1, const string & s2) { return *s1.inner != *s2.inner; }
179bool ?>? (const string & s1, const string & s2) { return *s1.inner >  *s2.inner; }
180bool ?>=?(const string & s1, const string & s2) { return *s1.inner >= *s2.inner; }
181bool ?<=?(const string & s1, const string & s2) { return *s1.inner <= *s2.inner; }
182bool ?<? (const string & s1, const string & s2) { return *s1.inner <  *s2.inner; }
183
184int  strcmp(const string & s1, const char * s2) { return strcmp(*s1.inner, s2 ); }
185bool ?==?(const string & s1, const char * s2) { return *s1.inner == s2; }
186bool ?!=?(const string & s1, const char * s2) { return *s1.inner != s2; }
187bool ?>? (const string & s1, const char * s2) { return *s1.inner >  s2; }
188bool ?>=?(const string & s1, const char * s2) { return *s1.inner >= s2; }
189bool ?<=?(const string & s1, const char * s2) { return *s1.inner <= s2; }
190bool ?<? (const string & s1, const char * s2) { return *s1.inner <  s2; }
191
192int  strcmp(const char * s1, const string & s2) { return strcmp( s1, *s2.inner); }
193bool ?==?(const char * s1, const string & s2) { return s1 == *s2.inner; }
194bool ?!=?(const char * s1, const string & s2) { return s1 != *s2.inner; }
195bool ?>? (const char * s1, const string & s2) { return s1 >  *s2.inner; }
196bool ?>=?(const char * s1, const string & s2) { return s1 >= *s2.inner; }
197bool ?<=?(const char * s1, const string & s2) { return s1 <= *s2.inner; }
198bool ?<? (const char * s1, const string & s2) { return s1 <  *s2.inner; }
[f450f2f]199
200
201////////////////////////////////////////////////////////
202// Getter
203
[6264087]204size_t size(const string & s) {
[681e12f]205    return size( *s.inner );
[f450f2f]206}
207
208////////////////////////////////////////////////////////
209// Concatenation
210
[681e12f]211void ?+=?(string & s, char c) {
212    (*s.inner) += c;
[f450f2f]213}
214
[6264087]215void ?+=?(string & s, const string & s2) {
[f450f2f]216    (*s.inner) += (*s2.inner);
217}
218
[e891349]219void append(string & s, const string & s2, size_t maxlen) {
220    append( (*s.inner), (*s2.inner), maxlen );
221}
222
[681e12f]223void ?+=?(string & s, const char * c) {
224    (*s.inner) += c;
[f450f2f]225}
226
[e891349]227void append(string & s, const char * buffer, size_t bsize) {
228    append( (*s.inner), buffer, bsize );
229}
230
[681e12f]231string ?+?(const string & s, char c) {
[f450f2f]232    string ret = s;
[681e12f]233    ret += c;
[f450f2f]234    return ret;
235}
236
[6264087]237string ?+?(const string & s, const string & s2) {
[f450f2f]238    string ret = s;
239    ret += s2;
240    return ret;
241}
242
[6264087]243string ?+?(const char * s1, const char * s2) {
[f450f2f]244    string ret = s1;
245    ret += s2;
246    return ret;
247}
248
[681e12f]249string ?+?(const string & s, const char * c) {
[f450f2f]250    string ret = s;
[681e12f]251    ret += c;
[f450f2f]252    return ret;
253}
254
255////////////////////////////////////////////////////////
256// Repetition
257
[38951c31]258void ?*=?(string & s, size_t factor) {
259    (*s.inner) *= factor;
[f450f2f]260}
261
[38951c31]262string ?*?(const string & s, size_t factor) {
263    string ret = s;
264    ret *= factor;
265    return ret;
[479fbe3]266}
267
268string ?*?(char c, size_t factor) {
269    string ret = c;
[38951c31]270    ret *= factor;
271    return ret;
[f450f2f]272}
273
[479fbe3]274string ?*?(const char * s, size_t factor) {
275    string ret = s;
[38951c31]276    ret *= factor;
277    return ret;
[f450f2f]278}
279
280////////////////////////////////////////////////////////
281// Character access
282
[6264087]283char ?[?](const string & s, size_t index) {
[f450f2f]284    return (*s.inner)[index];
285}
286
[6264087]287string ?[?](string & s, size_t index) {
[f450f2f]288    string ret = { *s.inner, index, index + 1 };
289    return ret`shareEdits;
290}
291
292////////////////////////////////////////////////////////
293// Search
294
[6264087]295bool contains(const string & s, char ch) {
[f450f2f]296    return contains( *s.inner, ch );
297}
298
[6264087]299int find(const string & s, char search) {
[f450f2f]300    return find( *s.inner, search );
301}
302
[6264087]303int find(const string & s, const string & search) {
[f450f2f]304    return find( *s.inner, *search.inner );
305}
306
[6264087]307int find(const string & s, const char * search) {
[f450f2f]308    return find( *s.inner, search);
309}
310
[6264087]311int find(const string & s, const char * search, size_t searchsize) {
[f450f2f]312    return find( *s.inner, search, searchsize);
313}
314
[6264087]315int findFrom(const string & s, size_t fromPos, char search) {
[08ed947]316    return findFrom( *s.inner, fromPos, search );
317}
318
[6264087]319int findFrom(const string & s, size_t fromPos, const string & search) {
[08ed947]320    return findFrom( *s.inner, fromPos, *search.inner );
321}
322
[6264087]323int findFrom(const string & s, size_t fromPos, const char * search) {
[08ed947]324    return findFrom( *s.inner, fromPos, search );
325}
326
[6264087]327int findFrom(const string & s, size_t fromPos, const char * search, size_t searchsize) {
[08ed947]328    return findFrom( *s.inner, fromPos, search, searchsize );
329}
330
[6264087]331bool includes(const string & s, const string & search) {
[f450f2f]332    return includes( *s.inner, *search.inner );
333}
334
[6264087]335bool includes(const string & s, const char * search) {
[f450f2f]336    return includes( *s.inner, search );
337}
338
[6264087]339bool includes(const string & s, const char * search, size_t searchsize) {
[f450f2f]340    return includes( *s.inner, search, searchsize );
341}
342
[6264087]343bool startsWith(const string & s, const string & prefix) {
[f450f2f]344    return startsWith( *s.inner, *prefix.inner );
345}
346
[6264087]347bool startsWith(const string & s, const char * prefix) {
[f450f2f]348    return startsWith( *s.inner, prefix );
349}
350
[6264087]351bool startsWith(const string & s, const char * prefix, size_t prefixsize) {
[f450f2f]352    return startsWith( *s.inner, prefix, prefixsize );
353}
354
[6264087]355bool endsWith(const string & s, const string & suffix) {
[f450f2f]356    return endsWith( *s.inner, *suffix.inner );
357}
358
[6264087]359bool endsWith(const string & s, const char * suffix) {
[f450f2f]360    return endsWith( *s.inner, suffix );
361}
362
[6264087]363bool endsWith(const string & s, const char * suffix, size_t suffixsize) {
[f450f2f]364    return endsWith( *s.inner, suffix, suffixsize );
365}
366
367
368///////////////////////////////////////////////////////////////////////////
369// charclass, include, exclude
370
[681e12f]371void ?{}( charclass & s, const string & chars) {
372    (s.inner) { malloc() };
373    ?{}( *s.inner, *(const string_res *)chars.inner );
[f450f2f]374}
375
[681e12f]376void ?{}( charclass & s, const char * chars ) {
377    (s.inner) { malloc() };
378    ?{}( *s.inner, chars );
[f450f2f]379}
380
[681e12f]381void ?{}( charclass & s, const char * chars, size_t charssize ) {
382    (s.inner) { malloc() };
383    ?{}( *s.inner, chars, charssize );
[f450f2f]384}
385
[681e12f]386void ^?{}( charclass & s ) {
387    ^(*s.inner){};
388    free( s.inner );
389    s.inner = 0p;
[f450f2f]390}
391
392
[6264087]393int exclude(const string & s, const charclass & mask) {
[f450f2f]394    return exclude( *s.inner, *mask.inner );
395}
396/*
[6264087]397StrSlice exclude(string & s, const charclass & mask) {
[f450f2f]398}
399*/
400
[6264087]401int include(const string & s, const charclass & mask) {
[f450f2f]402    return include( *s.inner, *mask.inner );
403}
404
405/*
[6264087]406StrSlice include(string & s, const charclass & mask) {
[f450f2f]407}
408*/
409
Note: See TracBrowser for help on using the repository browser.