Objectively 1.0.0
Ultra-lightweight object oriented framework for GNU C.
String.h
Go to the documentation of this file.
1/*
2 * Objectively: Ultra-lightweight object oriented framework for GNU C.
3 * Copyright (C) 2014 Jay Dolan <jay@jaydolan.com>
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 *
18 * 2. Altered source versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software.
20 *
21 * 3. This notice may not be removed or altered from any source distribution.
22 */
23
24#pragma once
25
26#include <ctype.h>
27#include <wctype.h>
28#include <stdarg.h>
29
30#include <Objectively/Array.h>
31#include <Objectively/Data.h>
32
41typedef wchar_t Unicode;
42
46typedef enum {
56
57typedef struct StringInterface StringInterface;
58
69struct String {
70
75
80 StringInterface *interface;
81
85 char *chars;
86
90 size_t length;
91};
92
93typedef struct MutableString MutableString;
94
98struct StringInterface {
99
103 ObjectInterface objectInterface;
104
114 Order (*compareTo)(const String *self, const String *other, const Range range);
115
124 Array *(*componentsSeparatedByCharacters)(const String *self, const char *chars);
125
134 Array *(*componentsSeparatedByString)(const String *self, const String *string);
135
144 Data *(*getData)(const String *self, StringEncoding encoding);
145
154 _Bool (*hasPrefix)(const String *self, const String *prefix);
155
164 _Bool (*hasSuffix)(const String *self, const String *suffix);
165
176 String *(*initWithBytes)(String *self, const uint8_t *bytes, size_t length, StringEncoding encoding);
177
186 String *(*initWithCharacters)(String *self, const char *chars);
187
197 String *(*initWithContentsOfFile)(String *self, const char *path, StringEncoding encoding);
198
208 String *(*initWithData)(String *self, const Data *data, StringEncoding encoding);
209
218 String *(*initWithFormat)(String *self, const char *fmt, ...);
219
230 String *(*initWithMemory)(String *self, const ident mem, size_t length);
231
242 String *(*initWithVaList)(String *self, const char *fmt, va_list args);
243
250 String *(*lowercaseString)(const String *self);
251
258 MutableString *(*mutableCopy)(const String *self);
259
269 Range (*rangeOfCharacters)(const String *self, const char *chars, const Range range);
270
280 Range (*rangeOfString)(const String *self, const String *string, const Range range);
281
292 String *(*stringWithBytes)(const uint8_t *bytes, size_t length, StringEncoding encoding);
293
302 String *(*stringWithCharacters)(const char *chars);
303
313 String *(*stringWithContentsOfFile)(const char *path, StringEncoding encoding);
314
324 String *(*stringWithData)(const Data *data, StringEncoding encoding);
325
334 String *(*stringWithFormat)(const char *fmt, ...);
335
346 String *(*stringWithMemory)(const ident mem, size_t length);
347
356 String *(*substring)(const String *self, const Range range);
357
365 String *(*trimmedString)(const String *self);
366
373 String *(*uppercaseString)(const String *self);
374
384 _Bool (*writeToFile)(const String *self, const char *path, StringEncoding encoding);
385};
386
394
402
409
418
425OBJECTIVELY_EXPORT String *str(const char *fmt, ...);
426
432OBJECTIVELY_EXPORT char *strtrim(const char *s);
433
Immutable arrays.
static _Bool writeToFile(const Data *self, const char *path)
Definition: Data.c:232
Immutable data buffers.
static Order compareTo(const Date *self, const Date *other)
Definition: Date.c:74
static MutableData * data(void)
Definition: MutableData.c:75
static MutableString * string(void)
static Range rangeOfString(const String *self, const String *string, const Range range)
Definition: String.c:467
static _Bool hasPrefix(const String *self, const String *prefix)
Definition: String.c:251
static Range rangeOfCharacters(const String *self, const char *chars, const Range range)
Definition: String.c:441
static _Bool hasSuffix(const String *self, const String *suffix)
Definition: String.c:265
StringEncoding
Character encodings for Strings.
Definition: String.h:46
@ STRING_ENCODING_LATIN1
Definition: String.h:48
@ STRING_ENCODING_WCHAR
Definition: String.h:54
@ STRING_ENCODING_UTF32
Definition: String.h:52
@ STRING_ENCODING_MACROMAN
Definition: String.h:50
@ STRING_ENCODING_UTF16
Definition: String.h:51
@ STRING_ENCODING_ASCII
Definition: String.h:47
@ STRING_ENCODING_LATIN2
Definition: String.h:49
@ STRING_ENCODING_UTF8
Definition: String.h:53
OBJECTIVELY_EXPORT char * strtrim(const char *s)
Copies the given null-terminated C string, trimming leading and trailing whitespace.
Definition: String.c:752
wchar_t Unicode
The Unicode type.
Definition: String.h:41
void * ident
The identity type, similar to Objective-C id.
Definition: Types.h:49
#define OBJECTIVELY_EXPORT
Definition: Types.h:36
Order
Comparison constants.
Definition: Types.h:70
Immutable arrays.
Definition: Array.h:56
The runtime representation of a Class.
Definition: Class.h:95
Immutable data buffers.
Definition: Data.h:50
Mutable UTF-8 strings.
Definition: MutableString.h:40
Object is the root Class of The Objectively Class hierarchy.
Definition: Object.h:46
A location and length into contiguous collections.
Definition: Types.h:54
Immutable UTF-8 strings.
Definition: String.h:69
char * chars
The backing null-terminated UTF-8 encoded character array.
Definition: String.h:85
StringInterface * interface
The interface.
Definition: String.h:80
size_t length
The length of the String in bytes.
Definition: String.h:90
OBJECTIVELY_EXPORT StringEncoding StringEncodingForName(const char *name)
Definition: String.c:698
OBJECTIVELY_EXPORT const char * NameForStringEncoding(StringEncoding encoding)
Definition: String.c:674
Object object
The superclass.
Definition: String.h:74
Class * _String(void)
The String archetype.
Definition: String.c:654
OBJECTIVELY_EXPORT String * str(const char *fmt,...)
A convenience function for instantiating Strings.
Definition: String.c:739
OBJECTIVELY_EXPORT Order StringCompare(const ident a, const ident b)
A Comparator for sorting Strings.
Definition: String.c:721