Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio;
- immutable ubyte[dchar] charMapAA;
- shared static this()
- {
- charMapAA =
- [
- '\u20ac': 0x80, '\u201a': 0x82, '\u201e': 0x84, '\u2026': 0x85,
- '\u2020': 0x86, '\u2021': 0x87, '\u2030': 0x89, '\u0160': 0x8a,
- '\u2039': 0x8b, '\u015a': 0x8c, '\u0164': 0x8d, '\u017d': 0x8e,
- '\u0179': 0x8f, '\u2018': 0x91, '\u2019': 0x92, '\u201c': 0x93,
- '\u201d': 0x94, '\u2022': 0x95, '\u2013': 0x96, '\u2014': 0x97,
- '\u2122': 0x99, '\u0161': 0x9a, '\u203a': 0x9b, '\u015b': 0x9c,
- '\u0165': 0x9d, '\u017e': 0x9e, '\u017a': 0x9f, '\u00a0': 0xa0,
- '\u02c7': 0xa1, '\u02d8': 0xa2, '\u0141': 0xa3, '\u00a4': 0xa4,
- '\u0104': 0xa5, '\u00a6': 0xa6, '\u00a7': 0xa7, '\u00a8': 0xa8,
- '\u00a9': 0xa9, '\u015e': 0xaa, '\u00ab': 0xab, '\u00ac': 0xac,
- '\u00ad': 0xad, '\u00ae': 0xae, '\u017b': 0xaf, '\u00b0': 0xb0,
- '\u00b1': 0xb1, '\u02db': 0xb2, '\u0142': 0xb3, '\u00b4': 0xb4,
- '\u00b5': 0xb5, '\u00b6': 0xb6, '\u00b7': 0xb7, '\u00b8': 0xb8,
- '\u0105': 0xb9, '\u015f': 0xba, '\u00bb': 0xbb, '\u013d': 0xbc,
- '\u02dd': 0xbd, '\u013e': 0xbe, '\u017c': 0xbf, '\u0154': 0xc0,
- '\u00c1': 0xc1, '\u00c2': 0xc2, '\u0102': 0xc3, '\u00c4': 0xc4,
- '\u0139': 0xc5, '\u0106': 0xc6, '\u00c7': 0xc7, '\u010c': 0xc8,
- '\u00c9': 0xc9, '\u0118': 0xca, '\u00cb': 0xcb, '\u011a': 0xcc,
- '\u00cd': 0xcd, '\u00ce': 0xce, '\u010e': 0xcf, '\u0110': 0xd0,
- '\u0143': 0xd1, '\u0147': 0xd2, '\u00d3': 0xd3, '\u00d4': 0xd4,
- '\u0150': 0xd5, '\u00d6': 0xd6, '\u00d7': 0xd7, '\u0158': 0xd8,
- '\u016e': 0xd9, '\u00da': 0xda, '\u0170': 0xdb, '\u00dc': 0xdc,
- '\u00dd': 0xdd, '\u0162': 0xde, '\u00df': 0xdf, '\u0155': 0xe0,
- '\u00e1': 0xe1, '\u00e2': 0xe2, '\u0103': 0xe3, '\u00e4': 0xe4,
- '\u013a': 0xe5, '\u0107': 0xe6, '\u00e7': 0xe7, '\u010d': 0xe8,
- '\u00e9': 0xe9, '\u0119': 0xea, '\u00eb': 0xeb, '\u011b': 0xec,
- '\u00ed': 0xed, '\u00ee': 0xee, '\u010f': 0xef, '\u0111': 0xf0,
- '\u0144': 0xf1, '\u0148': 0xf2, '\u00f3': 0xf3, '\u00f4': 0xf4,
- '\u0151': 0xf5, '\u00f6': 0xf6, '\u00f7': 0xf7, '\u0159': 0xf8,
- '\u016f': 0xf9, '\u00fa': 0xfa, '\u0171': 0xfb, '\u00fc': 0xfc,
- '\u00fd': 0xfd, '\u0163': 0xfe, '\u02d9': 0xff
- ];
- }
- immutable wstring charMap =
- "\u20ac\ufffd\u201a\ufffd\u201e\u2026\u2020\u2021"~
- "\ufffd\u2030\u0160\u2039\u015a\u0164\u017d\u0179"~
- "\ufffd\u2018\u2019\u201c\u201d\u2022\u2013\u2014"~
- "\ufffd\u2122\u0161\u203a\u015b\u0165\u017e\u017a"~
- "\u00a0\u02c7\u02d8\u0141\u00a4\u0104\u00a6\u00a7"~
- "\u00a8\u00a9\u015e\u00ab\u00ac\u00ad\u00ae\u017b"~
- "\u00b0\u00b1\u02db\u0142\u00b4\u00b5\u00b6\u00b7"~
- "\u00b8\u0105\u015f\u00bb\u013d\u02dd\u013e\u017c"~
- "\u0154\u00c1\u00c2\u0102\u00c4\u0139\u0106\u00c7"~
- "\u010c\u00c9\u0118\u00cb\u011a\u00cd\u00ce\u010e"~
- "\u0110\u0143\u0147\u00d3\u00d4\u0150\u00d6\u00d7"~
- "\u0158\u016e\u00da\u0170\u00dc\u00dd\u0162\u00df"~
- "\u0155\u00e1\u00e2\u0103\u00e4\u013a\u0107\u00e7"~
- "\u010d\u00e9\u0119\u00eb\u011b\u00ed\u00ee\u010f"~
- "\u0111\u0144\u0148\u00f3\u00f4\u0151\u00f6\u00f7"~
- "\u0159\u016f\u00fa\u0171\u00fc\u00fd\u0163\u02d9";
- bool canEncode(dchar c)
- {
- if (c < 0x80) return true;
- if (c >= 0xFFFD) return false;
- foreach(wchar d;charMap) { if (c == d) return true; }
- return false;
- }
- bool canEncodeAA(dchar c)
- {
- if(c < 0x80) return true;
- if(c in charMapAA) return true;
- return false;
- }
- void main()
- {
- string text = "Příliš žluťoučký kůň";
- void f0()
- {
- foreach(ch; text)
- {
- canEncode(ch);
- }
- }
- void f1()
- {
- foreach(ch; text)
- {
- canEncodeAA(ch);
- }
- }
- import std.datetime;
- import std.conv;
- auto r = benchmark!(f0, f1)(10_000);
- writeln("Array:\t", to!Duration(r[0]));
- writeln("AA:\t", to!Duration(r[1]));
- }
- unittest
- {
- string text = "Příliš žluťoučký kůň";
- foreach(ch; text)
- {
- assert(canEncode(ch) == canEncodeAA(ch));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement