2014-09-26 16:15:07 -07:00
|
|
|
#ifndef RUNTIME_LENGTH_H_
|
|
|
|
|
#define RUNTIME_LENGTH_H_
|
|
|
|
|
|
2015-12-04 10:45:30 -08:00
|
|
|
#include "tree_sitter/parser.h"
|
2014-09-26 16:15:07 -07:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
static inline bool ts_length_is_unknown(TSLength self) {
|
|
|
|
|
return self.chars > 0 && self.bytes == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline void ts_length_set_unknown(TSLength *self) {
|
|
|
|
|
self->bytes = 0;
|
|
|
|
|
self->rows = 0;
|
|
|
|
|
self->columns = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 23:40:57 -07:00
|
|
|
static inline TSLength ts_length_min(TSLength len1, TSLength len2) {
|
|
|
|
|
return (len1.chars < len2.chars) ? len1 : len2;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
static inline TSLength ts_length_add(TSLength len1, TSLength len2) {
|
2014-09-26 16:31:30 -07:00
|
|
|
TSLength result;
|
|
|
|
|
result.chars = len1.chars + len2.chars;
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
2015-09-15 16:00:16 -07:00
|
|
|
result.bytes = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
result.rows = 0;
|
|
|
|
|
result.columns = result.chars;
|
|
|
|
|
} else {
|
|
|
|
|
result.bytes = len1.bytes + len2.bytes;
|
|
|
|
|
if (len2.rows == 0) {
|
|
|
|
|
result.rows = len1.rows;
|
|
|
|
|
result.columns = len1.columns + len2.columns;
|
|
|
|
|
} else {
|
|
|
|
|
result.rows = len1.rows + len2.rows;
|
|
|
|
|
result.columns = len2.columns;
|
|
|
|
|
}
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline TSLength ts_length_sub(TSLength len1, TSLength len2) {
|
2014-09-26 16:31:30 -07:00
|
|
|
TSLength result;
|
|
|
|
|
result.chars = len1.chars - len2.chars;
|
2015-09-15 16:00:16 -07:00
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
if (ts_length_is_unknown(len1) || ts_length_is_unknown(len2)) {
|
2015-09-15 16:00:16 -07:00
|
|
|
result.bytes = 0;
|
2015-12-04 20:20:29 -08:00
|
|
|
result.rows = 0;
|
|
|
|
|
result.columns = result.chars;
|
2015-11-30 12:56:10 -05:00
|
|
|
} else {
|
2015-12-04 20:20:29 -08:00
|
|
|
result.bytes = len1.bytes - len2.bytes;
|
|
|
|
|
if (len1.rows == len2.rows) {
|
|
|
|
|
result.rows = 0;
|
|
|
|
|
result.columns = len1.columns - len2.columns;
|
|
|
|
|
} else {
|
|
|
|
|
result.rows = len1.rows - len2.rows;
|
|
|
|
|
result.columns = len1.columns;
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
static inline TSLength ts_length_zero() {
|
2015-12-04 20:56:33 -08:00
|
|
|
return (TSLength){ 0, 0, 0, 0 };
|
2015-11-12 15:32:53 -05:00
|
|
|
}
|
|
|
|
|
|
2015-12-04 20:20:29 -08:00
|
|
|
static inline bool ts_length_eq(TSLength self, TSLength other) {
|
2015-12-04 20:56:33 -08:00
|
|
|
return self.bytes == other.bytes && self.chars == other.chars &&
|
|
|
|
|
self.rows == other.rows && self.columns == other.columns;
|
2014-09-26 16:31:30 -07:00
|
|
|
}
|
|
|
|
|
|
2014-09-26 16:15:07 -07:00
|
|
|
#endif
|