next up previous contents
Next: Functions on Type t_list Up: Primitives Previous: Functions on Type t_int   Contents

Functions on Type t_text

The first byte of a t_text value has index 0.

t_bool
operator==(t_text t1, t_text t2)

Returns true if t1 and t2 are identical byte sequences, and false otherwise.

t_bool
operator!=(t_text t1, t_text t2) =
  operator!(operator==(t1, t2))

t_text
operator+(t_text t1, t_text t2)

Returns the byte sequence formed by appending the byte sequence t2 to the byte sequence t1 (concatenation).

t_int
_length(t_text t)

Returns the number of bytes in the byte sequence t.

t_text
_elem(t_text t, t_int i)

If $0 \leq i < $ _length(t), returns a byte sequence of length 1 consisting of byte i of the byte sequence t. Otherwise, returns the empty byte sequence.

t_text
_sub(t_text t, t_int start = 0, t_int len = _length(t)) =
{
  int w = _length(t);
  int i = _min(_max(start, 0)), w);
  int j = _min(i + _max(len, 0), w);
  // 0 <= i <= j <= _length(t); extract [i..j)
  t_text r = "";
  for (; i < j; i++) r = operator+(r, _elem(t, i));
  return r;
}

Extracts from t and returns a byte sequence of length len beginning at byte start. Note the boundary cases defined by the pseudo-code; _sub produces a runtime error only if it is passed arguments of the wrong type.

t_int
_find(t_text t, t_text p, t_int start = 0) =
{
  int j = _length(t) - _length(p);
  if (j < 0) return -1;
  int i = _max(start, 0);
  if (i > j) return -1;
  for (; i <= j; i++) {
    int k = 0;
    while (k < _length(p) && 
           _elem(t, i+k) == _elem(p, k)) k++;
    if (k == _length(p)) return i;
  }
  return -1;
}

Finds the leftmost occurrence of p in t that begins at or after position start. Returns the index of the first byte of the occurrence, or -1 if none exists.

t_int
_findr(t_text t, t_text p, t_int start = 0) =
{
  int j = _length(t) - _length(p);
  if (j < 0) return -1;
  int i = _max(start, 0);
  if (i > j) return -1;
  for (; i <= j; j--) {
    int k = 0;
    while (k < _length(p) && 
           _elem(t, j+k) == _elem(p, k)) k++;
    if (k == _length(p)) return j;
  }
  return -1;
}

Finds the rightmost occurrence of p in t that begins at or after position start. Returns the index of the first byte of the occurrence, or -1 if none exists.


next up previous contents
Next: Functions on Type t_list Up: Primitives Previous: Functions on Type t_int   Contents
Allan Heydon, Roy Levin, Timothy Mann, Yuan Yu