tree-sitter/src/runtime/length.h

61 lines
1.3 KiB
C
Raw Normal View History

#ifndef RUNTIME_LENGTH_H_
#define RUNTIME_LENGTH_H_
#include <stdlib.h>
#include <stdbool.h>
2016-10-16 21:21:21 -07:00
#include "runtime/point.h"
#include "tree_sitter/runtime.h"
typedef struct {
uint32_t bytes;
uint32_t chars;
TSPoint extent;
} Length;
static inline bool length_has_unknown_chars(Length self) {
2016-09-13 13:08:52 -07:00
return self.bytes > 0 && self.chars == 0;
}
static inline void length_set_unknown_chars(Length *self) {
2016-09-13 13:08:52 -07:00
self->chars = 0;
}
static inline Length length_min(Length len1, Length len2) {
return (len1.bytes < len2.bytes) ? len1 : len2;
}
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;
result.extent = point_add(len1.extent, len2.extent);
2015-09-15 16:00:16 -07:00
if (length_has_unknown_chars(len1) || length_has_unknown_chars(len2)) {
2016-09-13 13:08:52 -07:00
result.chars = 0;
} else {
2016-09-13 13:08:52 -07:00
result.chars = len1.chars + len2.chars;
}
2015-09-15 16:00:16 -07:00
return result;
}
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;
result.extent = point_sub(len1.extent, len2.extent);
2015-09-15 16:00:16 -07: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
}
return result;
}
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
}
#endif