Advertisement
Guest User

Untitled

a guest
Apr 13th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 3.95 KB | None | 0 0
  1. import std.stdio;
  2.  
  3. immutable ubyte[dchar] charMapAA;
  4. shared static this()
  5. {
  6.     charMapAA =
  7.     [
  8.         '\u20ac': 0x80, '\u201a': 0x82, '\u201e': 0x84, '\u2026': 0x85,
  9.         '\u2020': 0x86, '\u2021': 0x87, '\u2030': 0x89, '\u0160': 0x8a,
  10.         '\u2039': 0x8b, '\u015a': 0x8c, '\u0164': 0x8d, '\u017d': 0x8e,
  11.         '\u0179': 0x8f, '\u2018': 0x91, '\u2019': 0x92, '\u201c': 0x93,
  12.         '\u201d': 0x94, '\u2022': 0x95, '\u2013': 0x96, '\u2014': 0x97,
  13.         '\u2122': 0x99, '\u0161': 0x9a, '\u203a': 0x9b, '\u015b': 0x9c,
  14.         '\u0165': 0x9d, '\u017e': 0x9e, '\u017a': 0x9f, '\u00a0': 0xa0,
  15.         '\u02c7': 0xa1, '\u02d8': 0xa2, '\u0141': 0xa3, '\u00a4': 0xa4,
  16.         '\u0104': 0xa5, '\u00a6': 0xa6, '\u00a7': 0xa7, '\u00a8': 0xa8,
  17.         '\u00a9': 0xa9, '\u015e': 0xaa, '\u00ab': 0xab, '\u00ac': 0xac,
  18.         '\u00ad': 0xad, '\u00ae': 0xae, '\u017b': 0xaf, '\u00b0': 0xb0,
  19.         '\u00b1': 0xb1, '\u02db': 0xb2, '\u0142': 0xb3, '\u00b4': 0xb4,
  20.         '\u00b5': 0xb5, '\u00b6': 0xb6, '\u00b7': 0xb7, '\u00b8': 0xb8,
  21.         '\u0105': 0xb9, '\u015f': 0xba, '\u00bb': 0xbb, '\u013d': 0xbc,
  22.         '\u02dd': 0xbd, '\u013e': 0xbe, '\u017c': 0xbf, '\u0154': 0xc0,
  23.         '\u00c1': 0xc1, '\u00c2': 0xc2, '\u0102': 0xc3, '\u00c4': 0xc4,
  24.         '\u0139': 0xc5, '\u0106': 0xc6, '\u00c7': 0xc7, '\u010c': 0xc8,
  25.         '\u00c9': 0xc9, '\u0118': 0xca, '\u00cb': 0xcb, '\u011a': 0xcc,
  26.         '\u00cd': 0xcd, '\u00ce': 0xce, '\u010e': 0xcf, '\u0110': 0xd0,
  27.         '\u0143': 0xd1, '\u0147': 0xd2, '\u00d3': 0xd3, '\u00d4': 0xd4,
  28.         '\u0150': 0xd5, '\u00d6': 0xd6, '\u00d7': 0xd7, '\u0158': 0xd8,
  29.         '\u016e': 0xd9, '\u00da': 0xda, '\u0170': 0xdb, '\u00dc': 0xdc,
  30.         '\u00dd': 0xdd, '\u0162': 0xde, '\u00df': 0xdf, '\u0155': 0xe0,
  31.         '\u00e1': 0xe1, '\u00e2': 0xe2, '\u0103': 0xe3, '\u00e4': 0xe4,
  32.         '\u013a': 0xe5, '\u0107': 0xe6, '\u00e7': 0xe7, '\u010d': 0xe8,
  33.         '\u00e9': 0xe9, '\u0119': 0xea, '\u00eb': 0xeb, '\u011b': 0xec,
  34.         '\u00ed': 0xed, '\u00ee': 0xee, '\u010f': 0xef, '\u0111': 0xf0,
  35.         '\u0144': 0xf1, '\u0148': 0xf2, '\u00f3': 0xf3, '\u00f4': 0xf4,
  36.         '\u0151': 0xf5, '\u00f6': 0xf6, '\u00f7': 0xf7, '\u0159': 0xf8,
  37.         '\u016f': 0xf9, '\u00fa': 0xfa, '\u0171': 0xfb, '\u00fc': 0xfc,
  38.         '\u00fd': 0xfd, '\u0163': 0xfe, '\u02d9': 0xff
  39.     ];
  40. }
  41.  
  42. immutable wstring charMap =
  43.     "\u20ac\ufffd\u201a\ufffd\u201e\u2026\u2020\u2021"~
  44.     "\ufffd\u2030\u0160\u2039\u015a\u0164\u017d\u0179"~
  45.     "\ufffd\u2018\u2019\u201c\u201d\u2022\u2013\u2014"~
  46.     "\ufffd\u2122\u0161\u203a\u015b\u0165\u017e\u017a"~
  47.     "\u00a0\u02c7\u02d8\u0141\u00a4\u0104\u00a6\u00a7"~
  48.     "\u00a8\u00a9\u015e\u00ab\u00ac\u00ad\u00ae\u017b"~
  49.     "\u00b0\u00b1\u02db\u0142\u00b4\u00b5\u00b6\u00b7"~
  50.     "\u00b8\u0105\u015f\u00bb\u013d\u02dd\u013e\u017c"~
  51.     "\u0154\u00c1\u00c2\u0102\u00c4\u0139\u0106\u00c7"~
  52.     "\u010c\u00c9\u0118\u00cb\u011a\u00cd\u00ce\u010e"~
  53.     "\u0110\u0143\u0147\u00d3\u00d4\u0150\u00d6\u00d7"~
  54.     "\u0158\u016e\u00da\u0170\u00dc\u00dd\u0162\u00df"~
  55.     "\u0155\u00e1\u00e2\u0103\u00e4\u013a\u0107\u00e7"~
  56.     "\u010d\u00e9\u0119\u00eb\u011b\u00ed\u00ee\u010f"~
  57.     "\u0111\u0144\u0148\u00f3\u00f4\u0151\u00f6\u00f7"~
  58.     "\u0159\u016f\u00fa\u0171\u00fc\u00fd\u0163\u02d9";
  59.    
  60. bool canEncode(dchar c)
  61. {
  62.     if (c < 0x80) return true;
  63.     if (c >= 0xFFFD) return false;
  64.     foreach(wchar d;charMap) { if (c == d) return true; }
  65.     return false;
  66. }
  67.  
  68. bool canEncodeAA(dchar c)
  69. {
  70.     if(c < 0x80) return true;
  71.     if(c in charMapAA) return true;
  72.     return false;
  73. }
  74.  
  75. void main()
  76. {
  77.     string text = "Příliš žluťoučký kůň";
  78.     void f0()
  79.     {
  80.         foreach(ch; text)
  81.         {
  82.             canEncode(ch);
  83.         }
  84.     }
  85.     void f1()
  86.     {
  87.         foreach(ch; text)
  88.         {
  89.             canEncodeAA(ch);
  90.         }
  91.     }
  92.    
  93.     import std.datetime;
  94.     import std.conv;
  95.    
  96.     auto r = benchmark!(f0, f1)(10_000);
  97.     writeln("Array:\t", to!Duration(r[0]));
  98.     writeln("AA:\t", to!Duration(r[1]));
  99. }
  100.  
  101. unittest
  102. {
  103.     string text = "Příliš žluťoučký kůň";
  104.     foreach(ch; text)
  105.     {
  106.         assert(canEncode(ch) == canEncodeAA(ch));
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement