2 * Eddie Kohler, Yandong Mao, Robert Morris
3 * Copyright (c) 2012-2013 President and Fellows of Harvard College
4 * Copyright (c) 2012-2013 Massachusetts Institute of Technology
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, subject to the conditions
9 * listed in the Masstree LICENSE file. These conditions include: you must
10 * preserve this copyright notice, and you cannot mention the copyright
11 * holders in advertising related to the Software without their permission.
12 * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
13 * notice is a summary of the Masstree LICENSE file; the license in that file
21 #include "straccum.hh"
25 check_straccum_utf8(StringAccum &sa, const char *in, int inlen,
26 const char *out, int outlen)
29 Encoding::UTF8Encoder<T> encoder;
30 sa.append_encoded(encoder, in, in + inlen);
31 return sa.length() == outlen && memcmp(sa.begin(), out, sa.length()) == 0;
36 check_straccum2_utf8(StringAccum &sa, const char *in, int inlen,
37 const char *out, int outlen)
40 memcpy(sa.reserve(inlen), in, inlen);
41 Encoding::UTF8Encoder<T> encoder;
42 sa.append_encoded(encoder, sa.begin(), sa.begin() + inlen);
43 return sa.length() == outlen && memcmp(sa.begin(), out, sa.length()) == 0;
47 main(int argc, char *argv[])
49 assert(String("abc").to_utf8() == "abc");
50 assert(String("").to_utf8() == "");
51 assert(String("ab\000cd", 5).to_utf8() == "abcd");
52 assert(String("\xc3\x9dHi!").to_utf8() == "\xc3\x9dHi!");
53 assert(String("\xddHi!").to_utf8() == "\xc3\x9dHi!");
54 assert(String("\xc3\x9dHi!\x9c").to_utf8() == "\xc3\x9dHi!\xc5\x93");
55 assert(String("ab\000c\x9c", 5).to_utf8() == "abc\xc5\x93");
56 assert(String("\xc3\x9dXY\000c\x9c", 7).to_utf8() == "\xc3\x9dXYc\xc5\x93");
59 check_straccum_utf8<Encoding::UTF8>(sa, "abc", 3, "abc", 3);
60 check_straccum_utf8<Encoding::UTF8>(sa, "", 0, "", 0);
61 check_straccum_utf8<Encoding::UTF8>(sa, "ab\000cd", 5, "ab\000cd", 5);
62 check_straccum_utf8<Encoding::UTF8NoNul>(sa, "ab\000cd", 5, "abcd", 4);
63 check_straccum_utf8<Encoding::UTF8>(sa, "\xc3\x9dHi!", 5, "\xc3\x9dHi!", 5);
64 check_straccum_utf8<Encoding::Windows1252>(sa, "\xddHi!", 4, "\xc3\x9dHi!", 5);
66 check_straccum2_utf8<Encoding::UTF8>(sa, "abc", 3, "abc", 3);
67 check_straccum2_utf8<Encoding::UTF8>(sa, "", 0, "", 0);
68 check_straccum2_utf8<Encoding::UTF8>(sa, "ab\000cd", 5, "ab\000cd", 5);
69 check_straccum2_utf8<Encoding::UTF8NoNul>(sa, "ab\000cd", 5, "abcd", 4);
70 check_straccum2_utf8<Encoding::UTF8>(sa, "\xc3\x9dHi!", 5, "\xc3\x9dHi!", 5);
71 check_straccum2_utf8<Encoding::Windows1252>(sa, "\xddHi!", 4, "\xc3\x9dHi!", 5);
75 if (strcmp(argv[1], "-") == 0)
77 else if (!(f = fopen(argv[1], "rb"))) {
78 perror("test_string");
83 size_t x = fread(sa.reserve(1024), 1, 1024, f);
86 String s = sa.take_string().to_utf8(String::utf_strip_bom);
87 fwrite(s.data(), 1, s.length(), stdout);