-
Notifications
You must be signed in to change notification settings - Fork 7
/
byte_range.c
87 lines (68 loc) · 1.63 KB
/
byte_range.c
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
/*
* The code is distributed under terms of the BSD license.
* Copyright (c) 2016 Alex Pankratov. All rights reserved.
*
* http://swapped.cc/bsd-license
*/
#include "byte_range.h"
#include <ctype.h>
#include <stddef.h>
void br_get_line(byte_range * buf, byte_range * line)
{
uint8_t * p;
line->ptr = buf->ptr;
for (p = line->ptr; p < buf->end; p++)
if (*p == '\r' || *p == '\n')
break;
line->end = p;
if (p+1 < buf->end && p[0] == '\r' && p[1] == '\n') /* \r\n */
buf->ptr = p+2;
else
if (p < buf->end)
buf->ptr = p+1;
else
buf->ptr = p;
}
void br_trim(byte_range * line)
{
for ( ; line->ptr < line->end; line->ptr++)
if (! isspace(line->ptr[0]))
break;
for ( ; line->ptr < line->end; line->end--)
if (! isspace(line->end[-1]))
break;
}
void br_to_lower(byte_range * blob)
{
uint8_t * p;
for (p = blob->ptr; p < blob->end; p++)
*p = tolower(*p);
}
/*
*
*/
uint8_t br_front(const byte_range * br)
{
return (br->ptr < br->end) ? br->ptr[0] : 0;
}
uint8_t br_back(const byte_range * br)
{
return (br->ptr < br->end) ? br->end[-1] : 0;
}
int br_compare(const byte_range * a, const byte_range * b)
{
int r = (a->end - a->ptr) - (b->end - b->ptr);
return r ? r : memcmp(a->ptr, b->ptr, a->end - a->ptr);
}
const uint8_t * br_search(const byte_range * haystack, const byte_range * needle)
{
size_t haystack_len = haystack->end - haystack->ptr;
size_t needle_len = needle->end - needle->ptr;
size_t i, n;
if (haystack_len < needle_len)
return NULL;
for (i=0, n=haystack_len-needle_len+1; i<n; i++)
if (memcmp(haystack->ptr+i, needle->ptr, needle_len) == 0)
return haystack->ptr+i;
return NULL;
}