summaryrefslogtreecommitdiff
path: root/enc/trans/newline.trans
blob: 409da1dc33e08a2f3963de3f1e0944bd17c78953 (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
#include "transcode_data.h"

<%
  map_normalize = {}
  map_normalize["{00-ff}"] = :func_so
%>

<%= transcode_generate_node(ActionMap.parse(map_normalize), "universal_newline") %>

static int
fun_so_universal_newline(rb_transcoding* t, const unsigned char* s, size_t l, unsigned char* o)
{
    int len;
    /*
      t->stateful[0] == 0       : normal
      t->stateful[0] == 1       : just after '\r'
    */
    if (s[0] == '\n') {
        if (t->stateful[0] == 0) {
            o[0] = '\n';
            len = 1;
        }
        else {
            len = 0;
        }
        t->stateful[0] = 0;
    }
    else if (s[0] == '\r') {
        o[0] = '\n';
        len = 1;
        t->stateful[0] = 1;
    }
    else {
        o[0] = s[0];
        len = 1;
        t->stateful[0] = 0;
    }
    return len;
}

static const rb_transcoder
rb_universal_newline = {
    "universal_newline", "", &universal_newline,
    1, /* input_unit_length */
    1, /* max_input */
    1, /* max_output */
    stateful_decoder, /* stateful_type */
    NULL, NULL, NULL, fun_so_universal_newline
};

<%
  map_crlf = {}
  map_crlf["{00-09,0b-ff}"] = :nomap
  map_crlf["0a"] = "0d0a"
%>

<%= transcode_generate_node(ActionMap.parse(map_crlf), "crlf_newline") %>

static const rb_transcoder
rb_crlf_newline = {
    "", "crlf_newline", &crlf_newline,
    1, /* input_unit_length */
    1, /* max_input */
    2, /* max_output */
    stateless_converter, /* stateful_type */
    NULL, NULL, NULL, NULL
};

<%
  map_cr = {}
  map_cr["{00-09,0b-ff}"] = :nomap
  map_cr["0a"] = "0d"
%>

<%= transcode_generate_node(ActionMap.parse(map_cr), "cr_newline") %>

static const rb_transcoder
rb_cr_newline = {
    "", "cr_newline", &cr_newline,
    1, /* input_unit_length */
    1, /* max_input */
    1, /* max_output */
    stateless_converter, /* stateful_type */
    NULL, NULL, NULL, NULL
};

void
Init_newline(void)
{
    rb_register_transcoder(&rb_universal_newline);
    rb_register_transcoder(&rb_crlf_newline);
    rb_register_transcoder(&rb_cr_newline);
}