diff options
Diffstat (limited to 'src/view.c')
| -rw-r--r-- | src/view.c | 113 |
1 files changed, 83 insertions, 30 deletions
@@ -644,27 +644,19 @@ teco_view_save_to_stdout(teco_view_t *ctx, GError **error) /** * Convert a glyph index to a byte offset as used by Scintilla. * - * This is optimized with the "line character index", + * This function is only for UTF-8 documents. + * It is optimized with the "line character index", * which must always be enabled in UTF-8 documents. * - * It is also used to validate glyph indexes. - * * @param ctx The view to operate on. * @param pos Position in glyphs/characters. * @return Position in bytes or -1 if pos is out of bounds. */ -gssize -teco_view_glyphs2bytes(teco_view_t *ctx, teco_int_t pos) +static gssize +teco_view_glyphs2bytes_unicode(teco_view_t *ctx, teco_int_t pos) { if (pos < 0) - return -1; /* invalid position */ - if (!pos) - return 0; - - if (!(teco_view_ssm(ctx, SCI_GETLINECHARACTERINDEX, 0, 0) & - SC_LINECHARACTERINDEX_UTF32)) - /* assume single-byte encoding */ - return pos <= teco_view_ssm(ctx, SCI_GETLENGTH, 0, 0) ? pos : -1; + return -1; sptr_t line = teco_view_ssm(ctx, SCI_LINEFROMINDEXPOSITION, pos, SC_LINECHARACTERINDEX_UTF32); @@ -675,19 +667,16 @@ teco_view_glyphs2bytes(teco_view_t *ctx, teco_int_t pos) } /** - * Convert byte offset to glyph/character index without bounds checking. + * Convert a bytes index to a glyph offset. + * + * This function is only for UTF-8 documents. + * It is optimized with the "line character index", + * which must always be enabled in UTF-8 documents. + * This function does \b not check for invalid positions. */ -teco_int_t -teco_view_bytes2glyphs(teco_view_t *ctx, gsize pos) +static teco_int_t +teco_view_bytes2glyphs_unicode(teco_view_t *ctx, gsize pos) { - if (!pos) - return 0; - - if (!(teco_view_ssm(ctx, SCI_GETLINECHARACTERINDEX, 0, 0) & - SC_LINECHARACTERINDEX_UTF32)) - /* assume single-byte encoding */ - return pos; - sptr_t line = teco_view_ssm(ctx, SCI_LINEFROMPOSITION, pos, 0); sptr_t line_bytes = teco_view_ssm(ctx, SCI_POSITIONFROMLINE, line, 0); return teco_view_ssm(ctx, SCI_INDEXPOSITIONFROMLINE, line, @@ -698,7 +687,7 @@ teco_view_bytes2glyphs(teco_view_t *ctx, gsize pos) #define TECO_RELATIVE_LIMIT 1024 /** - * Convert a glyph index relative to a byte position to + * Convert a glyph index relative to a position to * a byte position. * * Can be used to implement commands with relative character @@ -709,23 +698,87 @@ teco_view_bytes2glyphs(teco_view_t *ctx, gsize pos) * (as on exceedingly long lines). * * @param ctx The view to operate on. + * @param pos_glyphs Glyph position to start. + * It must be the same position as specified by pos. * @param pos Byte position to start. * @param n Number of glyphs/characters to the left (negative) or - * right (positive) of pos. + * right (positive) of pos_glyphs and pos. * @return Position in bytes or -1 if the resulting position is out of bounds. */ gssize -teco_view_glyphs2bytes_relative(teco_view_t *ctx, gsize pos, teco_int_t n) +teco_view_glyphs2bytes_rel(teco_view_t *ctx, teco_int_t pos_glyphs, gsize pos, teco_int_t n) { - if (!n) + if (n == 0) return pos; + if (pos_glyphs == -n) + return 0; + + if (!(teco_view_ssm(ctx, SCI_GETLINECHARACTERINDEX, 0, 0) & + SC_LINECHARACTERINDEX_UTF32)) { + /* assume single-byte encoding */ + g_assert(pos_glyphs == pos); + + sptr_t len = teco_view_ssm(ctx, SCI_GETLENGTH, 0, 0); + if (0 > (gssize)pos+n || (gssize)pos+n > len) + return -1; + + return pos+n; + } + /* NOTE: Does not work for n == G_MININT64. */ if (ABS(n) > TECO_RELATIVE_LIMIT) - return teco_view_glyphs2bytes(ctx, teco_view_bytes2glyphs(ctx, pos) + n); + return teco_view_glyphs2bytes_unicode(ctx, pos_glyphs+n); sptr_t res = teco_view_ssm(ctx, SCI_POSITIONRELATIVE, pos, n); /* SCI_POSITIONRELATIVE may return 0 even if the offset is valid */ - return res ? : n > 0 ? -1 : teco_view_bytes2glyphs(ctx, pos)+n >= 0 ? 0 : -1; + return res ? : n > 0 ? -1 : pos_glyphs + n >= 0 ? 0 : -1; +} + +/** + * Convert a byte index relative to a known position to + * a glyph position. + * + * Can be used to implement commands with relative character + * ranges. + * As an optimization, this always counts characters for deltas + * smaller than TECO_RELATIVE_LIMIT, so it will be fast + * even where the character-index based lookup is too slow + * (as on exceedingly long lines). + * + * @param ctx The view to operate on. + * @param pos_glyphs Glyph position to start. + * It must be the same position as specified by pos. + * @param pos Byte position to start. + * @param n Number of bytes to the left (negative) or + * right (positive) of pos_glyphs and pos. + * @return Position in glyphs or -1 if the resulting position is out of bounds. + */ +teco_int_t +teco_view_bytes2glyphs_rel(teco_view_t *ctx, teco_int_t pos_glyphs, gsize pos, gssize n) +{ + if (n == 0) + return pos_glyphs; + if (pos == -n) + return 0; + + sptr_t len = teco_view_ssm(ctx, SCI_GETLENGTH, 0, 0); + + if (0 > (gssize)pos+n || (gssize)pos+n > len) + return -1; + + if (!(teco_view_ssm(ctx, SCI_GETLINECHARACTERINDEX, 0, 0) & + SC_LINECHARACTERINDEX_UTF32)) { + /* assume single-byte encoding */ + g_assert(pos_glyphs == pos); + return pos_glyphs+n; + } + + /* NOTE: Does not work for n == G_MINSSIZE. */ + if (ABS(n) > TECO_RELATIVE_LIMIT) + return teco_view_bytes2glyphs_unicode(ctx, pos+n); + + return n < 0 ? pos_glyphs - teco_view_ssm(ctx, SCI_COUNTCHARACTERS, pos+n, pos) + : pos_glyphs + teco_view_ssm(ctx, SCI_COUNTCHARACTERS, pos, pos+n); } /** |
