summaryrefslogtreecommitdiff
path: root/src/lib/string.c
blob: 0c01fca824371debb6bd4ef6457ffa931195e800 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* 
 * Copyright (C) 2026 Peter Mikkelsen <petermikkelsen10@gmail.com>
 *
 * This file is part of aplwc.
 *
 * Aplwc is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Aplwc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with aplwc.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "aplwc_internal.h"

struct string *
chars_to_string(struct aplwc *aplwc, const char *chars, int len)
{
	const uint8_t *utf8;
	size_t bytecount, charcount;
	struct string *string;
	uint32_t c;
	uint8_t b;
	int seqlen, extra;

	utf8 = (const uint8_t *)chars;
	seqlen = extra = 0;
	for(bytecount = 0, charcount = 0; ((bytecount < (unsigned int)len) || (len == -1)) && utf8[bytecount]; bytecount++){
		b = utf8[bytecount];
		if(extra == 0){
			c = 0;
			if((b & 0x80) == 0){
				seqlen = 0;
				c |= (b & 0x7F);
			}else if((b & 0xE0) == 0xC0){
				seqlen = 1;
				c |= (b & 0x1F);
			}else if((b & 0xF0) == 0xE0){
				seqlen = 2;
				c |= (b & 0x0F);
			}else if((b & 0xF8) == 0xF0){
				seqlen = 3;
				c |= (b & 0x7);
			}else{
invalid:
				aplwc->last_error = APLWC_INVALID_UTF8;
				return NULL;
			}
			extra = seqlen;
		}else{
			extra--;
			c <<= 6;
			if((b & 0xC0) == 0x80)
				c |= (b & 0x3F);
			else
				goto invalid;	
		}

		if(extra == 0){
			charcount++;
			switch(seqlen){
			case 0:
				if(NEVER(c == 0) || NEVER(c > 0x7F))
					goto invalid;
				break;
			case 1:
				if((c < 0x80) || NEVER(c > 0x07FF))
					goto invalid;
				break;
			case 2:
				if((c < 0x0800) || NEVER(c > 0xFFFF))
					goto invalid;
				break;
			case 3:
				if((c < 0x010000) || NEVER(c > 0x10FFFF))
					goto invalid;
				break;
			}
		}
	}
	
	if(extra != 0)
		goto invalid;

	string = memory_alloc_string(aplwc, bytecount);
	if(string){
		string->bytes = bytecount;
		string->chars = charcount;
		memcpy(string->utf8, utf8, bytecount);
	}

	return string;
}

uint32_t
string_char(struct string *string, size_t offset)
{
	uint32_t c;
	size_t len, i;

	assert(offset <= string->bytes);
	if(offset == string->bytes)
		return INVALID_CHAR;

	c = 0;
	len = char_length(string, offset);
	switch(char_length(string, offset)){
	case 1:
		c |= string->utf8[offset] & 0x7F;
		break;
	case 2:
		c |= string->utf8[offset] & 0x1F;
		break;
	case 3:
		c |= string->utf8[offset] & 0x0F;
		break;
	case 4:
		c |= string->utf8[offset] & 0x07;
		break;
	}

	for(i = 1; i < len; i++){
		c <<= 6;
		c |= string->utf8[offset+i] & 0x3F;
	}

	return c;
}

size_t
char_length(struct string *string, size_t offset)
{
	uint8_t b;

	assert(offset < string->bytes);

	b = string->utf8[offset];
	assert(!((b & 0xC0) == 0x80));

	if((b & 0x80) == 0)
		return 1;
	else if((b & 0xE0) == 0xC0)
		return 2;
	else if((b & 0xF0) == 0xE0)
		return 3;
	else
		return 4;
}

int
string_compare(struct string *a, struct string *b)
{
	size_t i;

	for(i = 0; (i < a->bytes) && (i < b->bytes); i++){
		if(a->utf8[i] < b->utf8[i])
			return -1;
		else if(a->utf8[i] > b->utf8[i])
			return 1;
	}

	if(a->bytes < b->bytes)
		return -1;
	else if(a->bytes > b->bytes)
		return 1;
	else
		return 0;
}

struct string *
substring(struct aplwc *aplwc, struct string *s, size_t start, size_t length)
{
	size_t bytes, i;
	struct string *r;

	MEMPTRS(aplwc, 1);
	MEMPTR(0, s);

	bytes = 0;
	for(i = 0; i < length; i++)
		bytes += char_length(s, start+bytes);

	r = memory_alloc_string(aplwc, bytes);
	if(r){
		r->bytes = bytes;
		r->chars = length;
		memcpy(r->utf8, s->utf8+start, bytes);
	}

	MEMPTRS_EXIT(aplwc);
	return r;
}