summaryrefslogtreecommitdiff
path: root/enc/trans/newline.trans
blob: a200ec00a790e9979cb2a6a52b01087c3be0e48a (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
132
133
134
135
#include "transcode_data.h"

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

  transcode_generate_node(ActionMap.parse(map_normalize), "universal_newline")

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

  transcode_generate_node(ActionMap.parse(map_crlf), "crlf_newline")

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

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

<%= transcode_generated_code %>

#define STATE (sp[0])
#define NORMAL 0
#define JUST_AFTER_CR 1

/* no way to access this information, yet. */
#define NEWLINES_MET (sp[1])
#define MET_LF          0x01
#define MET_CRLF        0x02
#define MET_CR          0x04

static int
universal_newline_init(void *statep)
{
    unsigned char *sp = statep;
    STATE = NORMAL;
    NEWLINES_MET = 0;
    return 0;
}

static ssize_t
fun_so_universal_newline(void *statep, const unsigned char *s, size_t l, unsigned char *o, size_t osize)
{
    unsigned char *sp = statep;
    int len;
    if (s[0] == '\n') {
        if (STATE == NORMAL) {
            NEWLINES_MET |= MET_LF;
        }
        else { /* JUST_AFTER_CR */
            NEWLINES_MET |= MET_CRLF;
        }
        o[0] = '\n';
        len = 1;
        STATE = NORMAL;
    }
    else {
        len = 0;
        if (STATE == JUST_AFTER_CR) {
            o[0] = '\n';
            len = 1;
            NEWLINES_MET |= MET_CR;
        }
        if (s[0] == '\r') {
            STATE = JUST_AFTER_CR;
        }
        else {
            o[len++] = s[0];
            STATE = NORMAL;
        }
    }

    return len;
}

static ssize_t
universal_newline_finish(void *statep, unsigned char *o, size_t osize)
{
    unsigned char *sp = statep;
    int len = 0;
    if (STATE == JUST_AFTER_CR) {
        o[0] = '\n';
        len = 1;
        NEWLINES_MET |= MET_CR;
    }
    STATE = NORMAL;
    return len;
}

static const rb_transcoder
rb_universal_newline = {
    "", "universal_newline", universal_newline,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    2, /* max_output */
    asciicompat_converter, /* asciicompat_type */
    2, universal_newline_init, universal_newline_init, /* state_size, state_init, state_fini */
    NULL, NULL, NULL, fun_so_universal_newline,
    universal_newline_finish
};

static const rb_transcoder
rb_crlf_newline = {
    "", "crlf_newline", crlf_newline,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    2, /* max_output */
    asciicompat_converter, /* asciicompat_type */
    0, NULL, NULL, /* state_size, state_init, state_fini */
    NULL, NULL, NULL, NULL
};

static const rb_transcoder
rb_cr_newline = {
    "", "cr_newline", cr_newline,
    TRANSCODE_TABLE_INFO,
    1, /* input_unit_length */
    1, /* max_input */
    1, /* max_output */
    asciicompat_converter, /* asciicompat_type */
    0, NULL, NULL, /* state_size, state_init, state_fini */
    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);
}