summaryrefslogtreecommitdiff
path: root/vere/pkg/noun/jets/e/leer.c
blob: 8ffe372c0de2014c5278b13bea93381324090022 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/// @file

#include "jets/k.h"
#include "jets/q.h"
#include "jets/w.h"

#include "noun.h"

static u3_atom
_leer_cut(c3_w pos_w, c3_w len_w, u3_atom src)
{
  if ( 0 == len_w ) {
    return 0;
  }
  else {
    u3i_slab sab_u;
    u3i_slab_bare(&sab_u, 3, len_w);
    sab_u.buf_w[sab_u.len_w - 1] = 0;

    u3r_bytes(pos_w, len_w, sab_u.buf_y, src);

    return u3i_slab_mint_bytes(&sab_u);
  }
}

// Leaving the lore jet in place for backwards compatibility.
// TODO: remove u3[qw]e_lore (also from jet tree)

u3_noun
u3qe_lore(u3_atom lub)
{
  c3_w    len_w = u3r_met(3, lub);
  c3_w    pos_w = 0;
  u3_noun tez = u3_nul;

  while ( 1 ) {
    c3_w meg_w = 0;
    c3_y end_y;

    c3_y byt_y;
    while ( 1 ) {
      if ( pos_w >= len_w ) {
        byt_y = 0;
        end_y = c3y;
        break;
      }
      byt_y = u3r_byte(pos_w + meg_w, lub);

      if ( (10 == byt_y) || (0 == byt_y) ) {
        end_y = __(byt_y == 0);
        break;
      } else meg_w++;
    }

    if ((byt_y == 0) && ((pos_w + meg_w + 1) < len_w)) {
      return u3m_bail(c3__exit);
    }

    if ( !_(end_y) && pos_w >= len_w ) {
      return u3kb_flop(tez);
    }
    else {
      tez = u3nc(_leer_cut(pos_w, meg_w, lub), tez);
      if ( _(end_y) ) {
        return u3kb_flop(tez);
      }
      pos_w += (meg_w + 1);
    }
  }
}

u3_noun
u3we_lore(u3_noun cor)
{
  u3_noun lub;

  if ( (u3_none == (lub = u3r_at(u3x_sam, cor))) ||
       (c3n == u3ud(lub)) )
  {
    return u3m_bail(c3__fail);
  } else {
    return u3qe_lore(lub);
  }
}

u3_noun
u3qe_leer(u3_atom txt)
{
  u3_noun  pro;
  u3_noun* lit = &pro;

  {
    c3_w pos_w, i_w = 0, len_w = u3r_met(3, txt);
    u3_noun* hed;
    u3_noun* tel;

    while ( i_w < len_w ) {
      //  scan till end or newline
      //
      for ( pos_w = i_w; i_w < len_w; ++i_w ) {
        if ( 10 == u3r_byte(i_w, txt) ) {
          break;
        }
      }

      //  append to list
      //
      *lit = u3i_defcons(&hed, &tel);
      *hed = _leer_cut(pos_w, i_w - pos_w, txt);
      lit  = tel;

      i_w++;
    }
  }

  *lit = u3_nul;

  return pro;
}

u3_noun
u3we_leer(u3_noun cor)
{
  u3_noun txt = u3x_at(u3x_sam, cor);

  if ( c3n == u3ud(txt) ) {
    return u3m_bail(c3__fail);
  }

  return u3qe_leer(txt);
}