Module: RuVim::DisplayWidthExt
- Defined in:
- ext/ruvim/ruvim_ext.c
Class Method Summary collapse
-
.cell_width(*args) ⇒ Object
------------------------------------------------------------------.
-
.display_width(*args) ⇒ Object
------------------------------------------------------------------.
-
.expand_tabs(*args) ⇒ Object
------------------------------------------------------------------.
-
.set_ambiguous_width(w) ⇒ Object
------------------------------------------------------------------.
Class Method Details
.cell_width(*args) ⇒ Object
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 |
# File 'ext/ruvim/ruvim_ext.c', line 105
static VALUE
rb_cell_width(int argc, VALUE *argv, VALUE self)
{
VALUE ch, opts;
rb_scan_args(argc, argv, "1:", &ch, &opts);
if (NIL_P(ch) || (TYPE(ch) == T_STRING && RSTRING_LEN(ch) == 0))
return INT2FIX(1);
int col = 0, tabstop = 2;
if (!NIL_P(opts)) {
VALUE v;
static ID id_col, id_tabstop;
if (!id_col) {
id_col = rb_intern("col");
id_tabstop = rb_intern("tabstop");
}
v = rb_hash_lookup2(opts, ID2SYM(id_col), Qnil);
if (!NIL_P(v)) col = NUM2INT(v);
v = rb_hash_lookup2(opts, ID2SYM(id_tabstop), Qnil);
if (!NIL_P(v)) tabstop = NUM2INT(v);
}
if (TYPE(ch) != T_STRING) return INT2FIX(1);
const char *ptr = RSTRING_PTR(ch);
long len = RSTRING_LEN(ch);
if (len == 1 && ptr[0] == '\t') {
int w = tabstop - (col % tabstop);
if (w == 0) w = tabstop;
return INT2FIX(w);
}
/* Fast path: single-byte ASCII */
if (len == 1) return INT2FIX(1);
/* Decode first codepoint */
unsigned int code = rb_enc_codepoint_len(ptr, ptr + len, NULL,
rb_utf8_encoding());
return INT2FIX(codepoint_width(code));
}
|
.display_width(*args) ⇒ Object
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 |
# File 'ext/ruvim/ruvim_ext.c', line 152
static VALUE
rb_display_width(int argc, VALUE *argv, VALUE self)
{
VALUE str, opts;
rb_scan_args(argc, argv, "1:", &str, &opts);
int tabstop = 2, start_col = 0;
if (!NIL_P(opts)) {
VALUE v;
static ID id_tabstop, id_start_col;
if (!id_tabstop) {
id_tabstop = rb_intern("tabstop");
id_start_col = rb_intern("start_col");
}
v = rb_hash_lookup2(opts, ID2SYM(id_tabstop), Qnil);
if (!NIL_P(v)) tabstop = NUM2INT(v);
v = rb_hash_lookup2(opts, ID2SYM(id_start_col), Qnil);
if (!NIL_P(v)) start_col = NUM2INT(v);
}
if (NIL_P(str)) str = rb_str_new("", 0);
if (TYPE(str) != T_STRING) str = rb_String(str);
const char *ptr = RSTRING_PTR(str);
const char *end = ptr + RSTRING_LEN(str);
rb_encoding *enc = rb_utf8_encoding();
int col = start_col;
while (ptr < end) {
unsigned int code;
int clen = rb_enc_precise_mbclen(ptr, end, enc);
if (!MBCLEN_CHARFOUND_P(clen)) {
/* invalid byte — skip one byte, width 1 */
ptr++;
col++;
continue;
}
clen = MBCLEN_CHARFOUND_LEN(clen);
code = rb_enc_codepoint(ptr, end, enc);
ptr += clen;
if (code == '\t') {
int w = tabstop - (col % tabstop);
if (w == 0) w = tabstop;
col += w;
} else if (clen == 1) {
col++; /* ASCII */
} else {
col += codepoint_width(code);
}
}
return INT2FIX(col - start_col);
}
|
.expand_tabs(*args) ⇒ Object
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 |
# File 'ext/ruvim/ruvim_ext.c', line 212
static VALUE
rb_expand_tabs(int argc, VALUE *argv, VALUE self)
{
VALUE str, opts;
rb_scan_args(argc, argv, "1:", &str, &opts);
int tabstop = 2, start_col = 0;
if (!NIL_P(opts)) {
VALUE v;
static ID id_tabstop, id_start_col;
if (!id_tabstop) {
id_tabstop = rb_intern("tabstop");
id_start_col = rb_intern("start_col");
}
v = rb_hash_lookup2(opts, ID2SYM(id_tabstop), Qnil);
if (!NIL_P(v)) tabstop = NUM2INT(v);
v = rb_hash_lookup2(opts, ID2SYM(id_start_col), Qnil);
if (!NIL_P(v)) start_col = NUM2INT(v);
}
if (NIL_P(str)) str = rb_str_new("", 0);
if (TYPE(str) != T_STRING) str = rb_String(str);
const char *ptr = RSTRING_PTR(str);
const char *end = ptr + RSTRING_LEN(str);
rb_encoding *enc = rb_utf8_encoding();
int col = start_col;
VALUE out = rb_str_buf_new(RSTRING_LEN(str) + 32);
rb_enc_associate(out, enc);
while (ptr < end) {
if (*ptr == '\t') {
int w = tabstop - (col % tabstop);
if (w == 0) w = tabstop;
for (int i = 0; i < w; i++)
rb_str_cat(out, " ", 1);
col += w;
ptr++;
} else {
int clen = rb_enc_precise_mbclen(ptr, end, enc);
if (!MBCLEN_CHARFOUND_P(clen)) {
rb_str_cat(out, ptr, 1);
ptr++;
col++;
continue;
}
clen = MBCLEN_CHARFOUND_LEN(clen);
unsigned int code = rb_enc_codepoint(ptr, end, enc);
rb_str_cat(out, ptr, clen);
col += (clen == 1) ? 1 : codepoint_width(code);
ptr += clen;
}
}
return out;
}
|
.set_ambiguous_width(w) ⇒ Object
274 275 276 277 278 279 |
# File 'ext/ruvim/ruvim_ext.c', line 274
static VALUE
rb_set_ambiguous_width(VALUE self, VALUE w)
{
ambiguous_width = NUM2INT(w);
return w;
}
|