From 7f1d8ea7fbacb1d2f6cabc9d50777ee21cbf9a7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Sun, 7 Feb 2016 13:19:01 +0100 Subject: text-object: introduce text_object_number --- text-objects.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'text-objects.c') diff --git a/text-objects.c b/text-objects.c index b5dec88..3ea81e4 100644 --- a/text-objects.c +++ b/text-objects.c @@ -13,6 +13,8 @@ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include +#include #include #include #include "text-motions.h" @@ -300,6 +302,29 @@ Filerange text_object_range(Text *txt, size_t pos, int (*isboundary)(int)) { return text_range_new(start, it.pos); } +static int is_number(int c) { + return !(c == '-' || c == 'x' || c == 'X' || + ('0' <= c && c <= '9') || + ('a' <= c && c <= 'f') || ('A' <= c && c <= 'F')); +} + +Filerange text_object_number(Text *txt, size_t pos) { + char *buf, *err = NULL; + Filerange r = text_object_range(txt, pos, is_number); + if (!text_range_valid(&r)) + return r; + if (!(buf = text_bytes_alloc0(txt, r.start, text_range_size(&r)))) + return text_range_empty(); + errno = 0; + strtoll(buf, &err, 0); + if (errno || err == buf) + r = text_range_empty(); + else + r.end = r.start + (err - buf); + free(buf); + return r; +} + Filerange text_range_linewise(Text *txt, Filerange *rin) { Filerange rout = *rin; rout.start = text_line_begin(txt, rin->start); -- cgit v1.2.3