Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
Properties | Methods | Protected Attributes
StringReader Struct Reference

#include <StringReader.h>

Overview

The StringReader type.

Definition at line 47 of file StringReader.h.

Inheritance diagram for StringReader:
Object

Properties

char * head
 The StringReader head. More...
 
Object object
 The superclass. More...
 
Stringstring
 The String to read. More...
 
- Properties inherited from Object
Classclazz
 Every instance of Object begins with a pointer to its Class. More...
 

Methods

Class_StringReader (void)
 The StringReader archetype. More...
 
StringReaderinitWithCharacters (StringReader *self, const char *chars)
 Initializes this StringReader with the specified C string. More...
 
StringReaderinitWithString (StringReader *self, String *string)
 Initializes this StringReader with the specified String. More...
 
Unicode next (StringReader *self)
 
Unicode next (StringReader *self, StringReaderMode mode)
 Consumes the next Unicode code point in this StringReader using the given mode. More...
 
Unicode peek (StringReader *self)
 Peeks at the next Unicode code point from this StringReader without advancing head. More...
 
int read (StringReader *self)
 Reads a single Unicode code point from this StringReader. More...
 
int readToken (StringReader *self, const Unicode *charset, Unicode *stop)
 
StringreadToken (StringReader *self, const Unicode *charset, Unocide *stop)
 Reads characters from this StringReader until a character in charset is encountered. More...
 
void reset (StringReader *self)
 Resets this StringReader, placing the head before the beginning of the String. More...
 
- Methods inherited from Object
Class_Object (void)
 The Object archetype. More...
 
Objectcopy (const Object *self)
 Creates a shallow copy of this Object. More...
 
void dealloc (Object *self)
 Frees all resources held by this Object. More...
 
Stringdescription (const Object *self)
 
int hash (const Object *self)
 
Objectinit (Object *self)
 Initializes this Object. More...
 
_Bool isEqual (const Object *self, const Object *other)
 Tests equality of the other Object. More...
 
_Bool isKindOfClass (const Object *self, const Class *clazz)
 Tests for Class hierarchy membership. More...
 

Protected Attributes

StringReaderInterface * interface
 The interface. More...
 
- Protected Attributes inherited from Object
ObjectInterface * interface
 The interface. More...
 

Property Details

◆ head

char* StringReader::head

The StringReader head.

Definition at line 63 of file StringReader.h.

◆ interface

StringReaderInterface* StringReader::interface
protected

The interface.

Definition at line 58 of file StringReader.h.

◆ object

Object StringReader::object

The superclass.

Definition at line 52 of file StringReader.h.

◆ string

String* StringReader::string ( void  )

The String to read.

Definition at line 68 of file StringReader.h.

Method Details

◆ _StringReader()

Class * _StringReader ( void  )

The StringReader archetype.

Returns
The StringReader Class.

Definition at line 192 of file StringReader.c.

192 {
193 static Class *clazz;
194 static Once once;
195
196 do_once(&once, {
197 clazz = _initialize(&(const ClassDef) {
198 .name = "StringReader",
199 .superclass = _Object(),
200 .instanceSize = sizeof(StringReader),
201 .interfaceOffset = offsetof(StringReader, interface),
202 .interfaceSize = sizeof(StringReaderInterface),
204 });
205 });
206
207 return clazz;
208}
Class * _initialize(const ClassDef *def)
Initializes the given Class.
Definition: Class.c:91
static void initialize(Class *clazz)
Definition: StringReader.c:174
long Once
The Once type.
Definition: Once.h:37
#define do_once(once, block)
Executes the given block at most one time.
Definition: Once.h:43
ClassDefs are passed to _initialize via an archetype to initialize a Class.
Definition: Class.h:41
The runtime representation of a Class.
Definition: Class.h:95
Class * clazz
Every instance of Object begins with a pointer to its Class.
Definition: Object.h:51
Class * _Object(void)
The Object archetype.
Definition: Object.c:136
The StringReader type.
Definition: StringReader.h:47
StringReaderInterface * interface
The interface.
Definition: StringReader.h:58

◆ initWithCharacters()

StringReader * initWithCharacters ( StringReader self,
const char *  chars 
)

Initializes this StringReader with the specified C string.

Parameters
selfThe StringReader.
charsThe null-terminated C string to read.
Returns
The initialized StringReader, or NULL on error.

Definition at line 64 of file StringReader.c.

64 {
65
66 String *string = str(chars);
67
68 self = $(self, initWithString, string);
69
70 release(string);
71
72 return self;
73}
ident release(ident obj)
Atomically decrement the given Object's reference count. If the resulting reference count is 0,...
Definition: Class.c:196
String * str(const char *fmt,...)
Definition: String.c:739
Immutable UTF-8 strings.
Definition: String.h:69
StringReader * initWithString(StringReader *self, String *string)
Initializes this StringReader with the specified String.
Definition: StringReader.c:79

◆ initWithString()

StringReader * initWithString ( StringReader self,
String string 
)

Initializes this StringReader with the specified String.

Parameters
selfThe StringReader.
stringThe String to read.
Returns
The initialized StringReader, or NULL on error.

Definition at line 79 of file StringReader.c.

79 {
80
81 self = (StringReader *) super(Object, self, init);
82 if (self) {
83 self->string = retain(string);
84 $(self, reset);
85 }
86 return self;
87}
ident retain(ident obj)
Atomically increment the given Object's reference count.
Definition: Class.c:211
#define super(type, obj, method,...)
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
Object * init(Object *self)
Initializes this Object.
Definition: Object.c:83
void reset(StringReader *self)
Resets this StringReader, placing the head before the beginning of the String.
Definition: StringReader.c:165
String * string
The String to read.
Definition: StringReader.h:68

◆ next() [1/2]

Unicode next ( StringReader self)

◆ next() [2/2]

Unicode next ( StringReader self,
StringReaderMode  mode 
)

Consumes the next Unicode code point in this StringReader using the given mode.

Parameters
selfThe StringReader.
modeThe StringReaderMode.
Returns
The next Unicode code point, or -1 if the StringReader is exhausted.

Definition at line 93 of file StringReader.c.

93 {
94 Unicode c;
95
96 const int bytes = mbtowc(&c, self->head, MB_CUR_MAX);
97 if (bytes > 0) {
98 if (mode == StringReaderRead) {
99 self->head += bytes;
100 }
101 return c;
102 }
103
104 return READER_EOF;
105}
wchar_t Unicode
The Unicode type.
Definition: String.h:41
@ StringReaderRead
Definition: StringReader.h:35
#define READER_EOF
Definition: StringReader.h:38
char * head
The StringReader head.
Definition: StringReader.h:63

◆ peek()

Unicode peek ( StringReader self)

Peeks at the next Unicode code point from this StringReader without advancing head.

Parameters
selfThe StringReader.
Returns
The next Unicode code point, or -1 if the StringReader is exhausted.
Remarks
This method is shorthand for $(reader, next, StringReaderPeek).

Definition at line 111 of file StringReader.c.

111 {
112 return $(self, next, StringReaderPeek);
113}
@ StringReaderPeek
Definition: StringReader.h:34
Unicode next(StringReader *self)

◆ read()

Unicode read ( StringReader self)

Reads a single Unicode code point from this StringReader.

Parameters
selfThe StringReader.
Returns
The Unicode code point, or -1 if the StringReader is exhausted.
Remarks
This method is shorthand for $(reader, next, StringReaderRead).

◆ readToken() [1/2]

int readToken ( StringReader self,
const Unicode charset,
Unicode stop 
)

Definition at line 127 of file StringReader.c.

127 {
128
129 while (true) {
130 const Unicode c = $(self, peek);
131 if (c == READER_EOF || wcschr(charset, c) == NULL) {
132 break;
133 }
134 $(self, read);
135 }
136
137 const char *start = self->head;
138
139 while (true) {
140 const Unicode c = $(self, peek);
141 if (c == READER_EOF || wcschr(charset, c)) {
142 if (stop) {
143 *stop = c;
144 }
145 break;
146 }
147 $(self, read);
148 }
149
150 if (self->head - start > 0) {
151 const Range range = {
152 .location = start - self->string->chars,
153 .length = self->head - start
154 };
155 return $(self->string, substring, range);
156 }
157
158 return NULL;
159}
static void start(Operation *self)
Definition: Operation.c:171
static String * substring(const String *self, const Range range)
Definition: String.c:539
A location and length into contiguous collections.
Definition: Types.h:54
ssize_t location
The location.
Definition: Types.h:59
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
Unicode peek(StringReader *self)
Peeks at the next Unicode code point from this StringReader without advancing head.
Definition: StringReader.c:111
int read(StringReader *self)
Reads a single Unicode code point from this StringReader.

◆ readToken() [2/2]

String * readToken ( StringReader self,
const Unicode charset,
Unocide *  stop 
)

Reads characters from this StringReader until a character in charset is encountered.

Parameters
selfThe StringReader.
charsetThe array of potential stop characters.
stopOptionally returns the stop character from charset.
Returns
The token accumulated before stop, or NULL if the StringReader is exhausted.

◆ reset()

void reset ( StringReader self)

Resets this StringReader, placing the head before the beginning of the String.

Parameters
selfThe StringReader.

Definition at line 165 of file StringReader.c.

165 {
166 self->head = self->string->chars;
167}

The documentation for this struct was generated from the following files: