2014-09-26 16:15:07 -07:00
|
|
|
#ifndef RUNTIME_LENGTH_H_
|
|
|
|
|
#define RUNTIME_LENGTH_H_
|
|
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
#include <stdlib.h>
|
2014-09-26 16:15:07 -07:00
|
|
|
#include <stdbool.h>
|
2016-10-16 21:21:21 -07:00
|
|
|
#include "runtime/point.h"
|
2016-10-16 21:10:25 -07:00
|
|
|
#include "tree_sitter/runtime.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
|
2016-10-05 14:02:49 -07:00
|
|
|
typedef struct {
|
2016-11-14 12:15:24 -08:00
|
|
|
uint32_t bytes;
|
|
|
|
|
uint32_t chars;
|
2016-10-16 21:10:25 -07:00
|
|
|
TSPoint extent;
|
2016-11-09 20:59:05 -08:00
|
|
|
} Length;
|
2014-09-26 16:15:07 -07:00
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline bool length_has_unknown_chars(Length self) {
|
2016-09-13 13:08:52 -07:00
|
|
|
return self.bytes > 0 && self.chars == 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline void length_set_unknown_chars(Length *self) {
|
2016-09-13 13:08:52 -07:00
|
|
|
self->chars = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline Length length_min(Length len1, Length len2) {
|
2017-01-24 12:48:47 -08:00
|
|
|
return (len1.bytes < len2.bytes) ? len1 : len2;
|
2016-09-03 23:40:57 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline Length length_add(Length len1, Length len2) {
|
|
|
|
|
Length result;
|
2016-09-13 13:08:52 -07:00
|
|
|
result.bytes = len1.bytes + len2.bytes;
|
2016-11-09 20:59:05 -08:00
|
|
|
result.extent = point_add(len1.extent, len2.extent);
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
if (length_has_unknown_chars(len1) || length_has_unknown_chars(len2)) {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
} else {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = len1.chars + len2.chars;
|
2015-12-04 20:20:29 -08:00
|
|
|
}
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2014-09-26 16:31:30 -07:00
|
|
|
return result;
|
2014-09-26 16:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline Length length_sub(Length len1, Length len2) {
|
|
|
|
|
Length result;
|
2016-09-13 13:08:52 -07:00
|
|
|
result.bytes = len1.bytes - len2.bytes;
|
2016-11-09 20:59:05 -08:00
|
|
|
result.extent = point_sub(len1.extent, len2.extent);
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
if (length_has_unknown_chars(len1) || length_has_unknown_chars(len2)) {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = 0;
|
2015-11-30 12:56:10 -05:00
|
|
|
} else {
|
2016-09-13 13:08:52 -07:00
|
|
|
result.chars = len1.chars - len2.chars;
|
2015-11-30 12:56:10 -05:00
|
|
|
}
|
2015-11-18 16:34:50 -08:00
|
|
|
|
2014-09-26 16:31:30 -07:00
|
|
|
return result;
|
2014-09-26 16:15:07 -07:00
|
|
|
}
|
|
|
|
|
|
2016-11-09 20:59:05 -08:00
|
|
|
static inline Length length_zero() {
|
2017-08-08 10:47:59 -07:00
|
|
|
Length result = {0, 0, {0, 0}};
|
|
|
|
|
return result;
|
2015-11-12 15:32:53 -05:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
#endif
|