1#ifndef TREE_SITTER_LENGTH_H_
 2#define TREE_SITTER_LENGTH_H_
 3
 4#include <stdlib.h>
 5#include <stdbool.h>
 6#include "./point.h"
 7#include "api.h"
 8
 9typedef struct {
10  uint32_t bytes;
11  TSPoint extent;
12} Length;
13
14static const Length LENGTH_UNDEFINED = {0, {0, 1}};
15static const Length LENGTH_MAX = {UINT32_MAX, {UINT32_MAX, UINT32_MAX}};
16
17static inline bool length_is_undefined(Length length) {
18  return length.bytes == 0 && length.extent.column != 0;
19}
20
21static inline Length length_min(Length len1, Length len2) {
22  return (len1.bytes < len2.bytes) ? len1 : len2;
23}
24
25static inline Length length_add(Length len1, Length len2) {
26  Length result;
27  result.bytes = len1.bytes + len2.bytes;
28  result.extent = point_add(len1.extent, len2.extent);
29  return result;
30}
31
32static inline Length length_sub(Length len1, Length len2) {
33  Length result;
34  result.bytes = len1.bytes - len2.bytes;
35  result.extent = point_sub(len1.extent, len2.extent);
36  return result;
37}
38
39static inline Length length_zero(void) {
40  Length result = {0, {0, 0}};
41  return result;
42}
43
44static inline Length length_saturating_sub(Length len1, Length len2) {
45  if (len1.bytes > len2.bytes) {
46    return length_sub(len1, len2);
47  } else {
48    return length_zero();
49  }
50}
51
52#endif