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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use super::{catch_io, CompareError, Comparison};

use crate::byte_read::{ByteRead, IoByte};

use std::cmp::{Ord, Ordering};
use std::panic::{panic_any, AssertUnwindSafe};

pub fn try_normal_compare(
    std_reader: &mut impl ByteRead,
    user_reader: &mut impl ByteRead,
) -> Result<Comparison, CompareError> {
    catch_io(AssertUnwindSafe(move || {
        normal_compare(std_reader, user_reader)
    }))
    .map_err(CompareError::Io)
}

#[inline(never)]
fn normal_compare(std_reader: &mut impl ByteRead, user_reader: &mut impl ByteRead) -> Comparison {
    let mut std_byte = std_reader.next_byte();
    let mut user_byte = user_reader.next_byte();

    let mut ans = Comparison::AC;

    loop {
        if std_byte.is_eof() {
            return handle_eof(user_reader, user_byte, ans);
        }

        if user_byte.is_eof() {
            return handle_eof(std_reader, std_byte, ans);
        }

        let (a, b) = (std_byte.as_u8(), user_byte.as_u8());
        if a == b {
            let ret = poll_diff(std_reader, user_reader);
            std_byte = ret.0;
            user_byte = ret.1;
            continue;
        }

        if a == b'\n' {
            if !b.is_ascii_whitespace() {
                return Comparison::WA;
            }
            if poll_endline(user_reader) {
                std_byte = std_reader.next_byte();
                user_byte = user_reader.next_byte();
                continue;
            } else {
                return Comparison::WA;
            }
        }
        if b == b'\n' {
            if !a.is_ascii_whitespace() {
                return Comparison::WA;
            }
            if poll_endline(std_reader) {
                std_byte = std_reader.next_byte();
                user_byte = user_reader.next_byte();
                continue;
            } else {
                return Comparison::WA;
            }
        }

        let flaga = a.is_ascii_whitespace();
        let flagb = b.is_ascii_whitespace();

        // a != b
        // both of them are non-space
        if !flaga & !flagb {
            return Comparison::WA;
        }

        // a != b
        // both of them are not non-space
        if flaga {
            std_byte = poll_nonspace(std_reader);
        }
        if flagb {
            user_byte = poll_nonspace(user_reader);
        }

        if std_byte.is_eof() || user_byte.is_eof() {
            continue;
        }

        let (a, b) = (std_byte.as_u8(), user_byte.as_u8());
        let flaga = a == b'\n';
        let flagb = b == b'\n';

        if flaga & flagb {
            std_byte = std_reader.next_byte();
            user_byte = user_reader.next_byte();
            continue;
        }
        if flaga | flagb {
            return Comparison::WA;
        }
        if a == b {
            ans = Comparison::PE;
            std_byte = std_reader.next_byte();
            user_byte = user_reader.next_byte();
            continue;
        } else {
            return Comparison::WA;
        }
    }
}

#[inline(never)]
fn handle_eof(rhs: &mut impl ByteRead, rhs_byte: IoByte, ans: Comparison) -> Comparison {
    if rhs_byte.is_eof() {
        return ans;
    }
    if !rhs_byte.as_u8().is_ascii_whitespace() {
        return Comparison::WA;
    }
    if poll_eof(rhs) {
        ans
    } else {
        Comparison::WA
    }
}

#[inline]
fn poll_diff(lhs: &mut impl ByteRead, rhs: &mut impl ByteRead) -> (IoByte, IoByte) {
    {
        let lhs_buf = match lhs.fill_buf() {
            Ok(b) => b,
            Err(e) => panic_any(e),
        };
        let rhs_buf = match rhs.fill_buf() {
            Ok(b) => b,
            Err(e) => panic_any(e),
        };
        if lhs_buf.len() >= 8 && rhs_buf.len() >= 8 {
            unsafe {
                let lhs_buf = lhs_buf.get_unchecked(..8);
                let rhs_buf = rhs_buf.get_unchecked(..8);
                if lhs_buf == rhs_buf {
                    lhs.consume_unchecked(8);
                    rhs.consume_unchecked(8);
                }
            }
        }
    }

    let mut lhs_byte;
    let mut rhs_byte;
    let mut eq_cnt: usize = 0;
    let mut cmp_cnt: usize = 0;

    loop {
        lhs_byte = lhs.next_byte();
        rhs_byte = rhs.next_byte();
        cmp_cnt += 1;

        if cmp_cnt >= 1024 {
            break;
        }

        if lhs_byte == rhs_byte {
            eq_cnt += 1;
            if lhs_byte.is_eof() {
                return (lhs_byte, rhs_byte);
            }
        } else {
            return (lhs_byte, rhs_byte);
        }
    }

    loop {
        if cmp_cnt >= 1024 && eq_cnt > cmp_cnt * 255 / 256 {
            let len = diff_block(lhs, rhs);
            if len == 0 {
                eq_cnt = 0;
                cmp_cnt = 0;
            } else {
                eq_cnt += len;
                cmp_cnt += len;
            }
        }

        lhs_byte = lhs.next_byte();
        rhs_byte = rhs.next_byte();
        cmp_cnt += 1;

        if lhs_byte == rhs_byte {
            eq_cnt += 1;
            if lhs_byte.is_eof() {
                return (lhs_byte, rhs_byte);
            }
        } else {
            return (lhs_byte, rhs_byte);
        }
    }
}

#[inline]
fn diff_block(lhs: &mut impl ByteRead, rhs: &mut impl ByteRead) -> usize {
    let mut total: usize = 0;
    loop {
        let lhs_buf: &[u8] = match lhs.fill_buf() {
            Ok(b) => b,
            Err(e) => panic_any(e),
        };

        let rhs_buf: &[u8] = match rhs.fill_buf() {
            Ok(b) => b,
            Err(e) => panic_any(e),
        };

        let (lhs_buf, rhs_buf, len) = match lhs_buf.len().cmp(&rhs_buf.len()) {
            Ordering::Equal => (lhs_buf, rhs_buf, lhs_buf.len()),
            Ordering::Less => (lhs_buf, &rhs_buf[..lhs_buf.len()], lhs_buf.len()),
            Ordering::Greater => (&lhs_buf[..rhs_buf.len()], rhs_buf, rhs_buf.len()),
        };

        if len == 0 {
            break total;
        }

        if lhs_buf == rhs_buf {
            lhs.consume(len);
            rhs.consume(len);
            total += len;
            continue;
        } else {
            break 0;
        }
    }
}

/// poll until eof.
/// ensure that all chars remaining in `chars` are ascii whitespaces
#[inline]
fn poll_eof(reader: &mut impl ByteRead) -> bool {
    loop {
        let b = reader.next_byte();
        if b.is_eof() {
            return true;
        }
        if !b.as_u8().is_ascii_whitespace() {
            return false;
        }
    }
}

/// poll until b'\n'.
/// ensure that all chars remaining in `chars` line are ascii whitespaces
#[inline(always)]
fn poll_endline(reader: &mut impl ByteRead) -> bool {
    let mut b = reader.next_byte();
    loop {
        if b.is_eof() || b.as_u8() == b'\n' {
            return true;
        }
        if !b.as_u8().is_ascii_whitespace() {
            return false;
        }
        b = reader.next_byte();
    }
}

/// poll until b'\n' or non-space or EOF
#[inline(always)]
fn poll_nonspace(reader: &mut impl ByteRead) -> IoByte {
    loop {
        let b: IoByte = reader.next_byte();
        if b.is_eof() || b.as_u8() == b'\n' || !b.as_u8().is_ascii_whitespace() {
            return b;
        }
    }
}

#[test]
fn test_normal_comparer() {
    macro_rules! judge {
        ($ret:expr, $std:expr,$user:expr) => {{
            let mut std: &[u8] = $std.as_ref();
            let mut user: &[u8] = $user.as_ref();

            let ret = normal_compare(&mut std, &mut user);
            assert_eq!(ret, $ret);
        }};
    }

    use Comparison::*;

    judge!(WA, b"1", b"2");
    judge!(WA, b"1\r\n", b"2\n");
    judge!(PE, b"1\r3\n", b"1\t3\n");
    judge!(PE, b"1 3\n", b"1\t3\n");
    judge!(PE, b"1 3\n", b"1         3\n");
    judge!(PE, b"1 3\r\n", b"1         3\r\n");
    judge!(PE, b"1 3\r\n", b"1         3\n");
    judge!(PE, b"1 3\n", b"1         3\r\n");
    judge!(PE, b"1\r3\t4\n", b"1\r3\r4\r\n");
    judge!(AC, b"1 2\n3 4", b"1 2\r\n3 4\n");
    judge!(AC, b"1 2 \n3 4", b"1 2 \r\n3 4 \n");
    judge!(AC, b"\n", b"");
    judge!(AC, b"", b"\n");
    judge!(AC, b" \n", b" ");
    judge!(AC, b"1\n", b"1");
    judge!(AC, b"1 \n", b"1");
    judge!(AC, b"1 \n", b"1\n");
    judge!(AC, b"1\t\n", b"1\r\n");
    judge!(AC, b"1\r\n", b"1\r");
    judge!(AC, b"1 2  \n3 4", b"1 2    \t\n3 4");
    judge!(AC, b"1 2 \r\n3 4", b"1 2                  \r\n3 4");
    judge!(AC, b"1\r\n\r\n\r\n", b"1  ");
    judge!(AC, b"1\r\n2\r\n", b"1 \n2 \n");
    judge!(AC, b"1\r\n2\r\n", b"1 \n2\t\n");
    judge!(AC, b"\t\n1", b"\r\n1");
    judge!(WA, b"asd", b"qwe");
    judge!(PE, b" asd", b"  asd");
    judge!(WA, b" asd", b"\nasd");
    judge!(PE, b" asd  \n", b"\tasd  \n");
    judge!(WA, b" asd  2\n", b"\tasd  1\n");
    judge!(AC, b"1\r", b"1\t");
    judge!(WA, b"1\na", b"1\n");
    judge!(WA, b"1\n", b"1\na");
    judge!(WA, b"1a", b"1");
    judge!(WA, b"1", b"1a");
    judge!(WA, b"1a \nb", b"1  \nb");
    judge!(WA, b"1\naa", b"1\n");
    judge!(WA, b"1\n", b"1\naa");
    judge!(WA, b"1 a", b"1 ");
    judge!(WA, b"1 aa", b"1 ");
    judge!(WA, b"1 ", b"1 a");
    judge!(WA, b"1 ", b"1 aa");
    judge!(AC, b"1\n\n3\n", b"1\r\n  \r\n3\t\n");
    judge!(WA, b"1\n3\n", b"1\r\n  \r\n3\t\n");
}