Skip to content

Commit

Permalink
text-object: introduce text_object_number
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Feb 7, 2016
1 parent f850388 commit 7f1d8ea
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
25 changes: 25 additions & 0 deletions text-objects.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 <errno.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "text-motions.h"
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions text-objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Filerange text_object_single_quote(Text*, size_t pos);
Filerange text_object_backtick(Text*, size_t pos);
/* text object delimited by arbitrary chars for which isboundary returns non-zero */
Filerange text_object_range(Text*, size_t pos, int (*isboundary)(int));
/* a number in either decimal, hex or octal format */
Filerange text_object_number(Text*, size_t pos);

/* extend a range to cover whole lines */
Filerange text_range_linewise(Text*, Filerange*);
Expand Down

0 comments on commit 7f1d8ea

Please sign in to comment.