-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstring.c
More file actions
151 lines (129 loc) · 3.77 KB
/
Copy pathstring.c
File metadata and controls
151 lines (129 loc) · 3.77 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
/*
* Copyright 2017 Patrick O. Perry.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include "rutf8.h"
static void bytes_init(struct rutf8_bytes *bytes, SEXP charsxp);
static int text_init(struct utf8lite_text *text, SEXP charsxp);
void rutf8_string_init(struct rutf8_string *str, SEXP charsxp)
{
if (charsxp == NA_STRING) {
str->type = RUTF8_STRING_NONE;
} else if (!text_init(&str->value.text, charsxp)) {
str->type = RUTF8_STRING_TEXT;
} else {
bytes_init(&str->value.bytes, charsxp);
str->type = RUTF8_STRING_BYTES;
}
}
void bytes_init(struct rutf8_bytes *bytes, SEXP charsxp)
{
assert(charsxp != NA_STRING);
bytes->ptr = (const uint8_t *)CHAR(charsxp);
bytes->size = (size_t)XLENGTH(charsxp);
}
int text_init(struct utf8lite_text *text, SEXP charsxp)
{
const uint8_t *ptr;
size_t size;
cetype_t ce;
int err = 0;
assert(charsxp != NA_STRING);
ce = getCharCE(charsxp);
if (rutf8_encodes_utf8(ce)) {
ptr = (const uint8_t *)CHAR(charsxp);
size = (size_t)XLENGTH(charsxp);
} else if (ce == CE_LATIN1 || ce == CE_NATIVE) {
ptr = (const uint8_t *)rutf8_translate_utf8(charsxp);
size = strlen((const char *)ptr);
} else {
err = UTF8LITE_ERROR_INVAL; // bytes or other encoding
goto exit;
}
TRY(utf8lite_text_assign(text, ptr, size, 0, NULL));
exit:
return err;
}
int rutf8_string_width(const struct rutf8_string *str, int flags)
{
switch (str->type) {
case RUTF8_STRING_TEXT:
return rutf8_text_width(&str->value.text, flags);
case RUTF8_STRING_BYTES:
return rutf8_bytes_width(&str->value.bytes, flags);
default:
return -1;
}
}
int rutf8_string_lwidth(const struct rutf8_string *str, int flags,
int limit, int ellipsis)
{
switch (str->type) {
case RUTF8_STRING_TEXT:
return rutf8_text_lwidth(&str->value.text, flags, limit,
ellipsis);
case RUTF8_STRING_BYTES:
return rutf8_bytes_lwidth(&str->value.bytes, flags, limit);
default:
return -1;
}
}
int rutf8_string_rwidth(const struct rutf8_string *str, int flags,
int limit, int ellipsis)
{
switch (str->type) {
case RUTF8_STRING_TEXT:
return rutf8_text_rwidth(&str->value.text, flags,
limit, ellipsis);
case RUTF8_STRING_BYTES:
return rutf8_bytes_rwidth(&str->value.bytes, flags, limit);
default:
return -1;
}
}
void rutf8_string_render(struct utf8lite_render *r,
const struct rutf8_string *str,
int width, int quote, enum rutf8_justify_type justify)
{
switch (str->type) {
case RUTF8_STRING_TEXT:
rutf8_text_render(r, &str->value.text, width, quote, justify);
break;
case RUTF8_STRING_BYTES:
rutf8_bytes_render(r, &str->value.bytes, width, quote, justify);
break;
default:
break;
}
}
SEXP rutf8_string_format(struct utf8lite_render *r,
const struct rutf8_string *str,
int trim, int chars, enum rutf8_justify_type justify,
int quote, const char *ellipsis, size_t nellipsis,
int wellipsis, int flags, int width_max)
{
switch (str->type) {
case RUTF8_STRING_TEXT:
return rutf8_text_format(r, &str->value.text, trim, chars,
justify, quote, ellipsis, nellipsis,
wellipsis, flags, width_max);
case RUTF8_STRING_BYTES:
// always use ASCII for byte formatting; UTF-8 isn't allowed
return rutf8_bytes_format(r, &str->value.bytes, trim, chars,
justify, quote, flags, width_max);
default:
return NA_STRING;
}
}