Repository navigation
Expand file tree
/
Copy pathBufferUtils.c
More file actions
executable file
·289 lines (234 loc) · 9.55 KB
/
Copy pathBufferUtils.c
File metadata and controls
executable file
·289 lines (234 loc) · 9.55 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* BufferUtils.c
* Notation
*
* Created by Zachary Schneirov on 1/15/06.
*/
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
This file is part of Notational Velocity.
Notational Velocity 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.
Notational Velocity 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 Notational Velocity. If not, see <http://www.gnu.org/licenses/>. */
#include "BufferUtils.h"
#include <string.h>
#include <stdint.h>
static const unsigned char gsToLowerMap[256] = {
'\0', 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, '\t',
'\n', 0x0b, 0x0c, '\r', 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13,
0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
0x1e, 0x1f, ' ', '!', '"', '#', '$', '%', '&', '\'',
'(', ')', '*', '+', ',', '-', '.', '/', '0', '1',
'2', '3', '4', '5', '6', '7', '8', '9', ':', ';',
'<', '=', '>', '?', '@', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', '{', '|', '}', '~', 0x7f, 0x80, 0x81,
0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b,
0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95,
0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd,
0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1,
0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xdb,
0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5,
0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9,
0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff };
#if !defined(MIN)
#define MIN(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
#endif
#if !defined(MAX)
#define MAX(A,B) ({ __typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
#endif
static u_int32_t u8_nextchar(const char *s, size_t *i, size_t len);
char *replaceString(char *oldString, const char *newString) {
if (newString == oldString) return oldString;
size_t newLen = strlen(newString) + 1;
//realloc is smart enough to do better memory management than we can do right here
char *resizedString = (char*)realloc(oldString, newLen);
memmove(resizedString, newString, newLen);
return resizedString;
}
void _ResizeBuffer(void ***buffer, unsigned int objCount, unsigned int *bufObjCount, unsigned int elemSize) {
assert(buffer && bufObjCount);
if (*bufObjCount < objCount || !*buffer) {
*buffer = (void **)realloc(*buffer, elemSize * objCount);
*bufObjCount = objCount;
}
}
void modp_tolower_copy(char* dest, const char* str, int len) {
int i;
const uint8_t* ustr = (const uint8_t*) str;
const int leftover = len % sizeof(NSUInteger);
const int imax = len / sizeof(NSUInteger);
// Process word-sized chunks without assuming alignment (use memcpy)
const char* sp = str;
char* dp = dest;
for (i = 0; i != imax; ++i) {
NSUInteger eax, ebx;
memcpy(&eax, sp, sizeof(NSUInteger));
/*
* This is based on the algorithm by Paul Hsieh
* http://www.azillionmonkeys.com/qed/asmexample.html
*/
ebx = (0x7f7f7f7f7f7f7f7fllu & eax) + 0x2525252525252525llu;
ebx = (0x7f7f7f7f7f7f7f7fllu & ebx) + 0x1a1a1a1a1a1a1a1allu;
ebx = ((ebx & ~eax) >> 2) & 0x2020202020202020llu;
NSUInteger out = eax + ebx;
memcpy(dp, &out, sizeof(NSUInteger));
sp += sizeof(NSUInteger);
dp += sizeof(NSUInteger);
}
i = imax * (int)sizeof(NSUInteger);
dest = dp;
switch (leftover) {
case 7: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 6: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 5: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 4: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 3: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 2: *dest++ = (char) gsToLowerMap[ustr[i++]];
case 1: *dest++ = (char) gsToLowerMap[ustr[i]];
case 0: *dest = '\0';
}
}
static const u_int32_t offsetsFromUTF8[6] = {
0x00000000UL, 0x00003080UL, 0x000E2080UL,
0x03C82080UL, 0xFA082080UL, 0x82082080UL
};
#define isutf(c) (((c)&0xC0)!=0x80)
/* reads the next utf-8 sequence out of a string, updating an index; never reads beyond len */
static force_inline u_int32_t u8_nextchar(const char *s, size_t *i, size_t len) {
u_int32_t ch = 0;
size_t sz = 0;
if (*i >= len) return 0;
for (;;) {
ch <<= 6;
ch += (unsigned char)s[*i];
sz++;
// If next byte would be out of range, advance once and stop
if (*i + 1 >= len) { (*i)++; break; }
// If next byte starts a new UTF-8 sequence or is NUL, advance and stop
if (!s[*i + 1] || isutf(s[*i + 1])) { (*i)++; break; }
// Otherwise, consume continuation byte and continue
(*i)++;
}
ch -= offsetsFromUTF8[sz - 1];
return ch;
}
void replace_breaks_utf8(char *s, size_t up_to_len) {
//needed to detect NSLineSeparatorCharacter and NSParagraphSeparatorCharacter
if (!s) return;
size_t i = 0, lasti = 0;
u_int32_t c;
while (i < up_to_len && s[i]) {
c = u8_nextchar(s, &i, up_to_len);
//get rid of any kind of funky whitespace-esq character
if (c == 0x0009 || c == 0x000a || c == 0x000d || c == 0x0003 || c == 0x2029 || c == 0x2028 || c == 0x000c) {
//fill in the entire UTF sequence with spaces
char *cur_s = (char*)&s[lasti];
// printf("\n");
do {
// printf("%X ", (u_int32_t)*cur_s);
*cur_s = ' ';
} while (++cur_s < &s[i]);
}
lasti = i;
}
}
void replace_breaks(char *str, size_t up_to_len) {
//traverses string to up_to_len chars or NULL, whichever comes first
//replaces any occurance of \n, \r, or \t with a space
if (!str) return;
size_t i = 0;
int c;
char *s = str;
do {
c = *s;
if (c == 0x0009 || c == 0x000a || c == 0x000d || c == 0x0003 || c == 0x000c) {
*s = ' ';
}
} while (++i < up_to_len && *(s++) != 0);
}
int ContainsUInteger(const NSUInteger *uintArray, size_t count, NSUInteger auint) {
size_t i;
for (i=0; i<count; i++) {
if (uintArray[i] == auint) return 1;
}
return 0;
}
int ContainsHighAscii(const void *s1, size_t n) {
// Check in word-sized chunks without requiring alignment
register NSUInteger i, integerCount = n/sizeof(NSUInteger);
register NSUInteger pattern = 0x8080808080808080;
const unsigned char* base = (const unsigned char*)s1;
for (i = 0; i < integerCount; i++) {
NSUInteger chunk;
memcpy(&chunk, base + i * sizeof(NSUInteger), sizeof(NSUInteger));
if (pattern & chunk) {
return 1;
}
}
const unsigned char *charBuffer = (const unsigned char*)s1;
NSUInteger leftOverCharCount = n % sizeof(NSUInteger);
for (i = n - leftOverCharCount; i < n; i++) {
if (charBuffer[i] > 127) {
return 1;
}
}
return 0;
}
CFStringRef CFStringFromBase10Integer(int quantity) {
char *buffer = NULL;
if (asprintf(&buffer, "%d", quantity) < 0 || !buffer)
return nil;
//try to get on the fast path of __CFStringCreateImmutableFunnel3; most overhead will be in cfruntime allocation, anyway, though, so whatever.
CFStringEncoding encoding = CFStringGetSystemEncoding() == kCFStringEncodingMacRoman ? kCFStringEncodingMacRoman : kCFStringEncodingASCII;
return CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, buffer, encoding, kCFAllocatorDefault);
}
unsigned DumbWordCount(const void *s1, size_t len) {
unsigned count = len > 0;
//we could do a lot more here, but we don't.
const void *ptr = s1;
while ((ptr = memchr(ptr + 1, 0x20, len))) {
count++;
}
// printf("bacon: %u\n", count);
return count;
}
NSInteger genericSortContextLast(void* one, void* two, int (*context) (void*, void*)) {
return context(&one, &two);
}
#if 0
//this does not use the user's defined date styles
const double dayInSeconds = 86400.0;
enum {ThisDay = 0, NextDay, PriorDay};
CFStringRef GetRelativeDateStringFromTimeAndLocaleInfo(CFAbsoluteTime time, CFStringRef *designations, char **months) {
static CFAbsoluteTime currentDay = 0.0;
if (currentDay == 0.0)
currentDay = ceil(CFAbsoluteTimeGetCurrent() / dayInSeconds) * dayInSeconds;
CFGregorianDate unitsDate = CFAbsoluteTimeGetGregorianDate(time, NULL);
CFAbsoluteTime timeDay = ceil(time / dayInSeconds) * dayInSeconds;
if (timeDay == currentDay) {
return designations[ThisDay];
} else if (timeDay == currentDay + dayInSeconds) {
return designations[NextDay];
} else if (timeDay == currentDay - dayInSeconds) {
return designations[PriorDay];
}
return CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s %u, %u %u:%u %s"),
months[unitsDate.month], unitsDate.day, unitsDate.year, unitsDate.hour, unitsDate.minute, amppmStr);
}
#endif