compact_enc_det 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. data/ext/compact_enc_det/compact_enc_det/CMakeLists.txt +103 -0
  3. data/ext/compact_enc_det/compact_enc_det/LICENSE +202 -0
  4. data/ext/compact_enc_det/compact_enc_det/README.md +46 -0
  5. data/ext/compact_enc_det/compact_enc_det/autogen.sh +74 -0
  6. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det.cc +5719 -0
  7. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det.h +83 -0
  8. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_fuzz_test.cc +54 -0
  9. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_generated_tables.h +6326 -0
  10. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_generated_tables2.h +856 -0
  11. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_hint_code.cc +169 -0
  12. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_hint_code.h +45 -0
  13. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/compact_enc_det_unittest.cc +5260 -0
  14. data/ext/compact_enc_det/compact_enc_det/compact_enc_det/detail_head_string.inc +152 -0
  15. data/ext/compact_enc_det/compact_enc_det/util/basictypes.h +331 -0
  16. data/ext/compact_enc_det/compact_enc_det/util/case_insensitive_hash.h +88 -0
  17. data/ext/compact_enc_det/compact_enc_det/util/commandlineflags.h +39 -0
  18. data/ext/compact_enc_det/compact_enc_det/util/encodings/encodings.cc +891 -0
  19. data/ext/compact_enc_det/compact_enc_det/util/encodings/encodings.h +299 -0
  20. data/ext/compact_enc_det/compact_enc_det/util/encodings/encodings.pb.h +181 -0
  21. data/ext/compact_enc_det/compact_enc_det/util/encodings/encodings_unittest.cc +34 -0
  22. data/ext/compact_enc_det/compact_enc_det/util/languages/languages.cc +349 -0
  23. data/ext/compact_enc_det/compact_enc_det/util/languages/languages.h +381 -0
  24. data/ext/compact_enc_det/compact_enc_det/util/languages/languages.pb.h +191 -0
  25. data/ext/compact_enc_det/compact_enc_det/util/logging.h +25 -0
  26. data/ext/compact_enc_det/compact_enc_det/util/port.h +53 -0
  27. data/ext/compact_enc_det/compact_enc_det/util/string_util.h +61 -0
  28. data/ext/compact_enc_det/compact_enc_det/util/varsetter.h +66 -0
  29. data/ext/compact_enc_det/compact_enc_det.cc +100 -0
  30. data/ext/compact_enc_det/extconf.rb +20 -0
  31. data/lib/compact_enc_det/version.rb +3 -0
  32. data/lib/compact_enc_det.rb +2 -0
  33. metadata +106 -0
@@ -0,0 +1,152 @@
1
+ // Copyright 2016 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ ////////////////////////////////////////////////////////////////////////////////
16
+ // Produced by stringify.cc on 2007-09-28 09:13 from file i18n/encodings/compact_enc_det/tools/detail_head.ps
17
+ "%!PS-Adobe-2.0\n\n/inch {72 mul} def\n/cshow {dup stringwidth pop -2 div"
18
+ " 0 rmoveto show} def\n\n/lmargin 0.5 inch def\n/rmargin 8.5 inch def\n/t"
19
+ "margin 10.5 inch def\n/bmargin 0.5 inch def\n\n\n% set to N>=0 to track "
20
+ "ranked encoding N\n/track-me -1 def\n/track-me2 -1 def\n\n/columns 2 def"
21
+ "\n\n/lpi 18 def % lines per inch\n/lpc lpi 10 mul def "
22
+ " % lines per column\n/lpp lpc columns mul def % lines per page\n"
23
+ "/probw 3.0 inch def % probability width\n/probr 50 def "
24
+ " % probability range\n/widowlines lpi 2 idiv def % 1/2 inch widow a"
25
+ "t bottom\n/widowlines lpi def % 1 inch widow at bottom\n\n/lpg l"
26
+ "pi 2 idiv def % 1/2 inch spacing between groups\n\n/delc 4 inch "
27
+ "def\n/dell -1 inch lpi div def\n\n/next-line 0 def % 24 lines per i"
28
+ "nch, 240 per column\n\n/Cfont /Courier findfont 6 scalefont def\n/Hfont "
29
+ "/Helvetica findfont 6 scalefont def\nHfont setfont\n\n\n% simple string "
30
+ "hash -- sum the characters\n/strhash {\n /hstr exch def\n /h 0 def\n "
31
+ "0 1 hstr length 1 sub {/i exch def /h h hstr i get add def} for\n h\n}"
32
+ " def\n\n% convert pro at 30 per 2x to 0-2.5 inches spanning -50 to 0\n/p"
33
+ "rob2x {\n 30 idiv probr div probw mul neg probw add\n}def\n\n\n/cliptoc"
34
+ "olumn {\n % ====== MUST MATCH ME ======\n gsave\n lmargin tmargin mov"
35
+ "eto\n next-line lpc idiv delc mul 0 rmoveto\n -1 18 rmoveto 0 -10.5 in"
36
+ "ch rlineto delc 2 add 0 rlineto 0 10.5 inch rlineto closepath\n clip\n"
37
+ " newpath\n} def\n\n/endcliptocolumn {\n grestore\n % ====== MUST MATC"
38
+ "H ME ======\n} def\n\n\n\n/column-box {\n lmargin tmargin moveto\n nex"
39
+ "t-line 1 sub lpc idiv delc mul next-line 1 sub lpc mod 1 add dell mul r"
40
+ "moveto\n % box\n gsave -1.5 0 rmoveto 0 detail-count dell mul neg rmov"
41
+ "eto probw 3 add 0 rlineto 0.25 setlinewidth stroke grestore\n gsave -1"
42
+ ".5 0 rmoveto 0 detail-count dell mul neg rlineto 0.25 setlinewidth strok"
43
+ "e grestore\n gsave probw .8 mul 0 rmoveto 0 detail-count dell mul neg r"
44
+ "lineto 0.25 setlinewidth 0.8 setgray stroke grestore\n gsave probw .6 m"
45
+ "ul 0 rmoveto 0 detail-count dell mul neg rlineto 0.25 setlinewidth 0.8 s"
46
+ "etgray stroke grestore\n gsave probw .4 mul 0 rmoveto 0 detail-count de"
47
+ "ll mul neg rlineto 0.25 setlinewidth 0.8 setgray stroke grestore\n gsav"
48
+ "e probw .2 mul 0 rmoveto 0 detail-count dell mul neg rlineto 0.25 setlin"
49
+ "ewidth 0.8 setgray stroke grestore\n gsave probw 1.5 add 0 rmoveto 0 de"
50
+ "tail-count dell mul neg rlineto 0.25 setlinewidth stroke grestore\n} def"
51
+ "\n\n\n/IncrementLine {\n /incr exch def\n /next-line next-line incr ad"
52
+ "d def\n next-line lpc ge next-line incr sub lpc lt and {\n % We "
53
+ "just went to the top of column 2; redo clip\n endcliptocolumn % M"
54
+ "UST match\n column-box\n /next-line lpc def\n cliptocolumn "
55
+ " % MUST match\n } if\n next-line lpp ge {\n % We just went to the"
56
+ " top of column 3; start new page column 1\n endcliptocolumn % MUS"
57
+ "T match\n column-box\n showpage\n Hfont setfont\n /next-line"
58
+ " 0 def\n show-pageno\n cliptocolumn % MUST match\n } if\n}"
59
+ " def\n\n/IncrementLineOutside {\n /incr exch def\n /next-line next-lin"
60
+ "e incr add def\n next-line lpc ge next-line incr sub lpc lt and {\n"
61
+ " % We just went to the top of column 2\n /next-line lpc def\n } i"
62
+ "f\n next-line lpp ge {\n % We just went to the top of column 3; star"
63
+ "t new page column 1\n showpage\n Hfont setfont\n /next-line 0 d"
64
+ "ef\n show-pageno\n } if\n} def\n\n/NextColumn {\n lpc 1 sub Increme"
65
+ "ntLine\n} def\n\n/NextPage {\n lpp 1 sub IncrementLine\n} def\n\n% Up"
66
+ "on entry, we are OUTSIDE the clip\n/start-detail {\n /d-title exch def\n"
67
+ "\n % align >= 1 inch at bottom of column, and/or start new column\n lp"
68
+ "c next-line lpc mod sub widowlines lt {\n % Start at top of a column\n"
69
+ " next-line lpc ge {\n % Start new page\n showpage\n Hf"
70
+ "ont setfont\n /next-line 0 def\n show-pageno\n } {\n %"
71
+ " Start new column\n /next-line lpc def\n } ifelse\n } if\n\n l"
72
+ "margin tmargin moveto\n next-line lpc idiv delc mul next-line lpc mod "
73
+ "dell mul rmoveto\n gsave d-title show grestore\n 0 dell rmoveto\n 1 1"
74
+ " 4 {/j exch def gsave probw j mul 5 div -2 rmoveto 50 j 10 mul sub 20 st"
75
+ "ring cvs cshow grestore} for\n 2 IncrementLineOutside\n /detail-count "
76
+ "1 def\n cliptocolumn % MUST match\n /d-array [] def\n} def\n"
77
+ "\n/size-detail {\n /d-names exch def\n /d-size exch def\n % zero sums"
78
+ "\n /sums d-size array def\n 0 1 d-size 1 sub {/i exch def sums i 0 pu"
79
+ "t} for\n /old-d-max 0 def\n /colors d-size array def\n 0 1 d-size 1 s"
80
+ "ub {/i exch def colors i i 3 mul 17 mod 17 div put} for\n %0 1 d-size "
81
+ "1 sub {/i exch def colors i d-names i get strhash 3 mul 17 mod 17 div p"
82
+ "ut} for\n %0 1 d-size 1 sub {/i exch def ( ) show colors i get 20 stri"
83
+ "ng cvs show} for\n} def\n\n/count-detail {\n /detail-total-count exch d"
84
+ "ef\n % if total-count >= one column, start at top of a column\n detail"
85
+ "-total-count lpp ge {\n % Start new page\n NextPage\n } {\n de"
86
+ "tail-total-count lpc ge {\n % Start new column\n NextColumn\n "
87
+ " } if\n } ifelse\n} def\n\n% highlight next entry with underbar\n/do-"
88
+ "flag {\ngsave\n setrgbcolor\n lmargin tmargin moveto\n next-line lpc "
89
+ "idiv delc mul next-line lpc mod dell mul rmoveto\n 0 -2 rmoveto\n pro"
90
+ "bw 0 rlineto\n 0 dell neg rlineto\n probw neg 0 rlineto\n closepath\n"
91
+ " fill\ngrestore\n} def\n\n/do-detail-e {\n /d-array exch def\n /d-enc"
92
+ " exch def\n /d-label exch def\n /d-max -999999 def\n\n lmargin tmargi"
93
+ "n moveto\n next-line lpc idiv delc mul next-line lpc mod dell mul rmov"
94
+ "eto\n 0.25 setlinewidth\n\n % show label, using encoding color\n gsav"
95
+ "e\n probw 2 add -2 rmoveto\n detail-count 1 sub 2 mod 0.25 inch mu"
96
+ "l 0 rmoveto\n % ([) show detail-count 20 string cvs show (] ) show\n "
97
+ " d-enc 0 lt {\n 0 setgray\n }{\n colors d-enc get 1 .8 se"
98
+ "thsbcolor\n } ifelse\n d-label show\n grestore\n % For -prune- d"
99
+ "raw horizontal line\n d-label length 8 gt {d-label 4 get (p) 0 get eq d"
100
+ "-label 5 get (r) 0 get eq and {\n /prune-val d-label cvi def\n /ne"
101
+ "wx prune-val 30 mul prob2x def\n gsave newx 6 rmoveto 0 -12 rlineto 1"
102
+ ".5 setlinewidth 0.8 setgray stroke grestore\n gsave probw 0 add 0 rli"
103
+ "neto 0.25 setlinewidth 0.8 setgray stroke grestore\n } if } if\n\n % t"
104
+ "rack max per new row\n 0 1 d-array length 1 sub {\n /i exch def\n "
105
+ " /sum sums i get d-array i get add def\n d-max sum lt {/d-max sum def"
106
+ "} if\n } for\n\n % draw line increments\n 0 1 d-array length 1 sub {\n"
107
+ " /i exch def\n detail-count 1 gt {\n /oldx old-d-max sums i g"
108
+ "et sub prob2x def\n } {\n /oldx 600 prob2x def\n } ifelse\n "
109
+ " /oldy dell neg def\n /newx d-max sums i get d-array i get add sub"
110
+ " prob2x def\n /newy 0 def\n gsave\n oldx oldy rmoveto\n newx"
111
+ " oldx sub newy oldy sub rlineto\n % if encoding is being tracked, ma"
112
+ "ke bold line\n i track-me eq\n {2 setlinewidth}\n {i track-"
113
+ "me2 eq {1.25 setlinewidth} {0.25 setlinewidth} ifelse}\n ifelse\n "
114
+ " colors i get 1 .8 sethsbcolor stroke\n grestore\n } for\n /detail"
115
+ "-count detail-count 1 add def\n\n % increment running total in sums, tr"
116
+ "ack max per row\n 0 1 d-array length 1 sub {\n /i exch def\n sums"
117
+ " i sums i get d-array i get add put\n } for\n /old-d-max d-max def\n"
118
+ "\n 1 IncrementLine\n} def\n\n\n/do-detail {\n /d-array exch def\n /d-"
119
+ "label exch def\n d-label -1 d-array do-detail-e\n} def\n\n% Upon exit, "
120
+ "we are outside the clip\n/end-detail {\n pop\n endcliptocolumn "
121
+ " % MUST match\n column-box\n\n % text labels\n 0 1 d-array length 1 s"
122
+ "ub {\n /i exch def\n gsave\n /newx old-d-max sums i get sub pro"
123
+ "b2x def\n newx 0 ge {\n newx 0 rmoveto\n currentpoint trans"
124
+ "late\n colors i get 1 .8 sethsbcolor\n gsave 0 dell neg rline"
125
+ "to 0.25 setlinewidth stroke grestore\n -60 rotate\n 0 -2 movet"
126
+ "o d-names i get show\n } if\n grestore\n } for\n d-array length "
127
+ "0 gt {\n lpg IncrementLineOutside\n } {\n lpg 4 idiv IncrementLin"
128
+ "eOutside\n } ifelse\n} def\n\n/do-src {\n/src exch def\n lmargin tmarg"
129
+ "in moveto\n next-line lpc idiv delc mul next-line lpc mod dell mul rmo"
130
+ "veto\n Cfont setfont\n src show\n Hfont setfont\n 1 IncrementLine\n}"
131
+ " def\n\n% Underline trigram in source text\n/do-highlight1 {\n /hl-colo"
132
+ "r exch def\n /hl-offset exch def\n /hl-line exch 1 sub 2 mul def\n gs"
133
+ "ave\n lmargin tmargin moveto\n next-line hl-line sub lpc idiv delc mul"
134
+ "\n next-line hl-line sub lpc mod dell mul rmoveto\n % Assume text is 6"
135
+ " chars in and 3.6 pts per char, but 2 chars per offset\n hl-offset 2 mu"
136
+ "l 6 add 3.6 mul 4 rmoveto\n\n 0 setgray 0.5 setlinewidth\n hl-color 1"
137
+ " eq {0 0 1 setrgbcolor} if % Latin1 blue\n hl-color 2 eq {1 0 1 setrgb"
138
+ "color} if % Latin2 magenta\n hl-color 3 eq {1 0.67 0 setrgbcolor} if "
139
+ "% Latin7 orange\n 18 -2 rlineto stroke\n grestore\n} def\n\n% Box trig"
140
+ "ram in source text\n/do-highlight2 {\n /hl-color exch def\n /hl-offset"
141
+ " exch def\n /hl-line exch 1 sub 2 mul def\n gsave\n lmargin tmargin m"
142
+ "oveto\n next-line hl-line sub lpc idiv delc mul\n next-line hl-line su"
143
+ "b lpc mod dell mul rmoveto\n % Assume text is 6 chars in and 3.6 pts pe"
144
+ "r char, but 2 chars per offset\n hl-offset 2 mul 6 add 3.6 mul 4 rmove"
145
+ "to\n\n 0 setgray 0.25 setlinewidth\n hl-color 1 eq {0 0 1 setrgbcolor}"
146
+ " if % Latin1 blue\n hl-color 2 eq {1 0 1 setrgbcolor} if % Latin2 mag"
147
+ "enta\n hl-color 3 eq {1 0.67 0 setrgbcolor} if % Latin7 orange\n -0.5"
148
+ " -0.5 rmoveto\n 22 0 rlineto\n 0 4 rlineto\n -11 2 rlineto\n -11 -2 "
149
+ "rlineto\n closepath\n stroke\n grestore\n} def\n\n/show-pageno {\ngsa"
150
+ "ve\nlmargin bmargin moveto 0 -12 rmoveto\n(Page ) show pageno 20 string "
151
+ "cvs show\ngrestore\n/pageno pageno 1 add def\n} def\n\n/pageno 1 def\nsh"
152
+ "ow-pageno\n%=============================\n\n\n"
@@ -0,0 +1,331 @@
1
+ // Copyright 2016 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ ////////////////////////////////////////////////////////////////////////////////
16
+
17
+ #ifndef UTIL_BASICTYPES_H_
18
+ #define UTIL_BASICTYPES_H_
19
+
20
+ #include <limits.h> // So we can set the bounds of our types
21
+ #include <stddef.h> // For size_t
22
+ #include <string.h> // for memcpy
23
+
24
+ #include "util/port.h" // Types that only need exist on certain systems
25
+
26
+ #ifndef COMPILER_MSVC
27
+ // stdint.h is part of C99 but MSVC doesn't have it.
28
+ #include <stdint.h> // For intptr_t.
29
+ #endif
30
+
31
+ typedef signed char schar;
32
+ typedef signed char int8;
33
+ typedef short int16;
34
+ // TODO(mbelshe) Remove these type guards. These are
35
+ // temporary to avoid conflicts with npapi.h.
36
+ #ifndef _INT32
37
+ #define _INT32
38
+ typedef int int32;
39
+ #endif
40
+
41
+ // The NSPR system headers define 64-bit as |long| when possible. In order to
42
+ // not have typedef mismatches, we do the same on LP64.
43
+ #if __LP64__
44
+ typedef long int64;
45
+ #else
46
+ typedef long long int64;
47
+ #endif
48
+
49
+ // NOTE: unsigned types are DANGEROUS in loops and other arithmetical
50
+ // places. Use the signed types unless your variable represents a bit
51
+ // pattern (eg a hash value) or you really need the extra bit. Do NOT
52
+ // use 'unsigned' to express "this value should always be positive";
53
+ // use assertions for this.
54
+
55
+ typedef unsigned char uint8;
56
+ typedef unsigned short uint16;
57
+ // TODO(mbelshe) Remove these type guards. These are
58
+ // temporary to avoid conflicts with npapi.h.
59
+ #ifndef _UINT32
60
+ #define _UINT32
61
+ typedef unsigned int uint32;
62
+ #endif
63
+
64
+ // See the comment above about NSPR and 64-bit.
65
+ #if __LP64__
66
+ typedef unsigned long uint64;
67
+ #else
68
+ typedef unsigned long long uint64;
69
+ #endif
70
+
71
+ // A type to represent a Unicode code-point value. As of Unicode 4.0,
72
+ // such values require up to 21 bits.
73
+ // (For type-checking on pointers, make this explicitly signed,
74
+ // and it should always be the signed version of whatever int32 is.)
75
+ typedef signed int char32;
76
+
77
+ const uint8 kuint8max = (( uint8) 0xFF);
78
+ const uint16 kuint16max = ((uint16) 0xFFFF);
79
+ const uint32 kuint32max = ((uint32) 0xFFFFFFFF);
80
+ const uint64 kuint64max = ((uint64) GG_LONGLONG(0xFFFFFFFFFFFFFFFF));
81
+ const int8 kint8min = (( int8) 0x80);
82
+ const int8 kint8max = (( int8) 0x7F);
83
+ const int16 kint16min = (( int16) 0x8000);
84
+ const int16 kint16max = (( int16) 0x7FFF);
85
+ const int32 kint32min = (( int32) 0x80000000);
86
+ const int32 kint32max = (( int32) 0x7FFFFFFF);
87
+ const int64 kint64min = (( int64) GG_LONGLONG(0x8000000000000000));
88
+ const int64 kint64max = (( int64) GG_LONGLONG(0x7FFFFFFFFFFFFFFF));
89
+
90
+ // A macro to disallow the copy constructor and operator= functions
91
+ // This should be used in the private: declarations for a class
92
+ #define DISALLOW_COPY_AND_ASSIGN(TypeName) \
93
+ TypeName(const TypeName&); \
94
+ void operator=(const TypeName&)
95
+
96
+ // An older, deprecated, politically incorrect name for the above.
97
+ #define DISALLOW_EVIL_CONSTRUCTORS(TypeName) DISALLOW_COPY_AND_ASSIGN(TypeName)
98
+
99
+ // A macro to disallow all the implicit constructors, namely the
100
+ // default constructor, copy constructor and operator= functions.
101
+ //
102
+ // This should be used in the private: declarations for a class
103
+ // that wants to prevent anyone from instantiating it. This is
104
+ // especially useful for classes containing only static methods.
105
+ #define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
106
+ TypeName(); \
107
+ DISALLOW_COPY_AND_ASSIGN(TypeName)
108
+
109
+ // The arraysize(arr) macro returns the # of elements in an array arr.
110
+ // The expression is a compile-time constant, and therefore can be
111
+ // used in defining new arrays, for example. If you use arraysize on
112
+ // a pointer by mistake, you will get a compile-time error.
113
+
114
+ // This template function declaration is used in defining arraysize.
115
+ // Note that the function doesn't need an implementation, as we only
116
+ // use its type.
117
+ template <typename T, size_t N>
118
+ char (&ArraySizeHelper(T (&array)[N]))[N];
119
+
120
+ // That gcc wants both of these prototypes seems mysterious. VC, for
121
+ // its part, can't decide which to use (another mystery). Matching of
122
+ // template overloads: the final frontier.
123
+ #ifndef _MSC_VER
124
+ template <typename T, size_t N>
125
+ char (&ArraySizeHelper(const T (&array)[N]))[N];
126
+ #endif
127
+
128
+ #define arraysize(array) (sizeof(ArraySizeHelper(array)))
129
+
130
+
131
+ // Use implicit_cast as a safe version of static_cast or const_cast
132
+ // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
133
+ // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
134
+ // a const pointer to Foo).
135
+ // When you use implicit_cast, the compiler checks that the cast is safe.
136
+ // Such explicit implicit_casts are necessary in surprisingly many
137
+ // situations where C++ demands an exact type match instead of an
138
+ // argument type convertable to a target type.
139
+ //
140
+ // The From type can be inferred, so the preferred syntax for using
141
+ // implicit_cast is the same as for static_cast etc.:
142
+ //
143
+ // implicit_cast<ToType>(expr)
144
+ //
145
+ // implicit_cast would have been part of the C++ standard library,
146
+ // but the proposal was submitted too late. It will probably make
147
+ // its way into the language in the future.
148
+ template<typename To, typename From>
149
+ inline To implicit_cast(From const &f) {
150
+ return f;
151
+ }
152
+
153
+ // The COMPILE_ASSERT macro can be used to verify that a compile time
154
+ // expression is true. For example, you could use it to verify the
155
+ // size of a static array:
156
+ //
157
+ // COMPILE_ASSERT(arraysize(content_type_names) == CONTENT_NUM_TYPES,
158
+ // content_type_names_incorrect_size);
159
+ //
160
+ // or to make sure a struct is smaller than a certain size:
161
+ //
162
+ // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
163
+ //
164
+ // The second argument to the macro is the name of the variable. If
165
+ // the expression is false, most compilers will issue a warning/error
166
+ // containing the name of the variable.
167
+
168
+ template <bool>
169
+ struct CompileAssert {
170
+ };
171
+
172
+ #undef COMPILE_ASSERT
173
+ #define COMPILE_ASSERT(expr, msg) \
174
+ typedef CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
175
+
176
+ // Implementation details of COMPILE_ASSERT:
177
+ //
178
+ // - COMPILE_ASSERT works by defining an array type that has -1
179
+ // elements (and thus is invalid) when the expression is false.
180
+ //
181
+ // - The simpler definition
182
+ //
183
+ // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
184
+ //
185
+ // does not work, as gcc supports variable-length arrays whose sizes
186
+ // are determined at run-time (this is gcc's extension and not part
187
+ // of the C++ standard). As a result, gcc fails to reject the
188
+ // following code with the simple definition:
189
+ //
190
+ // int foo;
191
+ // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
192
+ // // not a compile-time constant.
193
+ //
194
+ // - By using the type CompileAssert<(bool(expr))>, we ensures that
195
+ // expr is a compile-time constant. (Template arguments must be
196
+ // determined at compile-time.)
197
+ //
198
+ // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
199
+ // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
200
+ //
201
+ // CompileAssert<bool(expr)>
202
+ //
203
+ // instead, these compilers will refuse to compile
204
+ //
205
+ // COMPILE_ASSERT(5 > 0, some_message);
206
+ //
207
+ // (They seem to think the ">" in "5 > 0" marks the end of the
208
+ // template argument list.)
209
+ //
210
+ // - The array size is (bool(expr) ? 1 : -1), instead of simply
211
+ //
212
+ // ((expr) ? 1 : -1).
213
+ //
214
+ // This is to avoid running into a bug in MS VC 7.1, which
215
+ // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
216
+
217
+
218
+ // MetatagId refers to metatag-id that we assign to
219
+ // each metatag <name, value> pair..
220
+ typedef uint32 MetatagId;
221
+
222
+ // Argument type used in interfaces that can optionally take ownership
223
+ // of a passed in argument. If TAKE_OWNERSHIP is passed, the called
224
+ // object takes ownership of the argument. Otherwise it does not.
225
+ enum Ownership {
226
+ DO_NOT_TAKE_OWNERSHIP,
227
+ TAKE_OWNERSHIP
228
+ };
229
+
230
+ // bit_cast<Dest,Source> is a template function that implements the
231
+ // equivalent of "*reinterpret_cast<Dest*>(&source)". We need this in
232
+ // very low-level functions like the protobuf library and fast math
233
+ // support.
234
+ //
235
+ // float f = 3.14159265358979;
236
+ // int i = bit_cast<int32>(f);
237
+ // // i = 0x40490fdb
238
+ //
239
+ // The classical address-casting method is:
240
+ //
241
+ // // WRONG
242
+ // float f = 3.14159265358979; // WRONG
243
+ // int i = * reinterpret_cast<int*>(&f); // WRONG
244
+ //
245
+ // The address-casting method actually produces undefined behavior
246
+ // according to ISO C++ specification section 3.10 -15 -. Roughly, this
247
+ // section says: if an object in memory has one type, and a program
248
+ // accesses it with a different type, then the result is undefined
249
+ // behavior for most values of "different type".
250
+ //
251
+ // This is true for any cast syntax, either *(int*)&f or
252
+ // *reinterpret_cast<int*>(&f). And it is particularly true for
253
+ // conversions betweeen integral lvalues and floating-point lvalues.
254
+ //
255
+ // The purpose of 3.10 -15- is to allow optimizing compilers to assume
256
+ // that expressions with different types refer to different memory. gcc
257
+ // 4.0.1 has an optimizer that takes advantage of this. So a
258
+ // non-conforming program quietly produces wildly incorrect output.
259
+ //
260
+ // The problem is not the use of reinterpret_cast. The problem is type
261
+ // punning: holding an object in memory of one type and reading its bits
262
+ // back using a different type.
263
+ //
264
+ // The C++ standard is more subtle and complex than this, but that
265
+ // is the basic idea.
266
+ //
267
+ // Anyways ...
268
+ //
269
+ // bit_cast<> calls memcpy() which is blessed by the standard,
270
+ // especially by the example in section 3.9 . Also, of course,
271
+ // bit_cast<> wraps up the nasty logic in one place.
272
+ //
273
+ // Fortunately memcpy() is very fast. In optimized mode, with a
274
+ // constant size, gcc 2.95.3, gcc 4.0.1, and msvc 7.1 produce inline
275
+ // code with the minimal amount of data movement. On a 32-bit system,
276
+ // memcpy(d,s,4) compiles to one load and one store, and memcpy(d,s,8)
277
+ // compiles to two loads and two stores.
278
+ //
279
+ // I tested this code with gcc 2.95.3, gcc 4.0.1, icc 8.1, and msvc 7.1.
280
+ //
281
+ // WARNING: if Dest or Source is a non-POD type, the result of the memcpy
282
+ // is likely to surprise you.
283
+
284
+ template <class Dest, class Source>
285
+ inline Dest bit_cast(const Source& source) {
286
+ // Compile time assertion: sizeof(Dest) == sizeof(Source)
287
+ // A compile error here means your Dest and Source have different sizes.
288
+ // typedef char VerifySizesAreEqual [sizeof(Dest) == sizeof(Source) ? 1 : -1];
289
+
290
+ Dest dest;
291
+ memcpy(&dest, &source, sizeof(dest));
292
+ return dest;
293
+ }
294
+
295
+ // The following enum should be used only as a constructor argument to indicate
296
+ // that the variable has static storage class, and that the constructor should
297
+ // do nothing to its state. It indicates to the reader that it is legal to
298
+ // declare a static instance of the class, provided the constructor is given
299
+ // the base::LINKER_INITIALIZED argument. Normally, it is unsafe to declare a
300
+ // static variable that has a constructor or a destructor because invocation
301
+ // order is undefined. However, IF the type can be initialized by filling with
302
+ // zeroes (which the loader does for static variables), AND the destructor also
303
+ // does nothing to the storage, AND there are no virtual methods, then a
304
+ // constructor declared as
305
+ // explicit MyClass(base::LinkerInitialized x) {}
306
+ // and invoked as
307
+ // static MyClass my_variable_name(base::LINKER_INITIALIZED);
308
+ namespace base {
309
+ enum LinkerInitialized { LINKER_INITIALIZED };
310
+ } // base
311
+
312
+ // UnaligndLoad32 is put here instead of util/port.h to
313
+ // avoid the circular dependency between port.h and basictypes.h
314
+ // ARM does not support unaligned memory access.
315
+ #if defined(ARCH_CPU_X86_FAMILY)
316
+ // x86 and x86-64 can perform unaligned loads/stores directly;
317
+ inline uint32 UnalignedLoad32(const void* p) {
318
+ return *reinterpret_cast<const uint32*>(p);
319
+ }
320
+ #else
321
+ #define NEED_ALIGNED_LOADS
322
+ // If target architecture does not support unaligned loads and stores,
323
+ // use memcpy version of UNALIGNED_LOAD32.
324
+ inline uint32 UnalignedLoad32(const void* p) {
325
+ uint32 t;
326
+ memcpy(&t, reinterpret_cast<const uint8*>(p), sizeof(t));
327
+ return t;
328
+ }
329
+
330
+ #endif
331
+ #endif // UTIL_BASICTYPES_H_
@@ -0,0 +1,88 @@
1
+ // Copyright 2016 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ ////////////////////////////////////////////////////////////////////////////////
16
+
17
+ #ifndef UTIL_CASE_INSENSITIVE_HASH_H_
18
+ #define UTIL_CASE_INSENSITIVE_HASH_H_
19
+
20
+ #include <ctype.h>
21
+ #include <stddef.h>
22
+ #ifndef _MSC_VER
23
+ #include <strings.h>
24
+ #endif
25
+
26
+ #include <string>
27
+
28
+ #include "util/basictypes.h"
29
+ #include "util/string_util.h"
30
+
31
+ // Functors for hashing c-strings with case-insensitive semantics.
32
+ struct CStringCaseHash {
33
+ size_t operator()(const char *str) const {
34
+ unsigned long hash_val = 0;
35
+ while (*str) {
36
+ hash_val = 5*hash_val + tolower(*str);
37
+ str++;
38
+ }
39
+ return (size_t)hash_val;
40
+ }
41
+ };
42
+
43
+ struct CStringCaseEqual {
44
+ bool operator()(const char *str1, const char *str2) const {
45
+ return !base::strcasecmp(str1, str2);
46
+ }
47
+ };
48
+
49
+ // These functors, in addition to being case-insensitive, ignore all
50
+ // non-alphanumeric characters. This is useful when we want all variants of
51
+ // a string -- where variants can differ in puncutation and whitespace -- to
52
+ // map to the same value.
53
+ struct CStringAlnumCaseHash {
54
+ size_t operator()(const char *str) const {
55
+ unsigned long hash_val = 0;
56
+ while (*str) {
57
+ if (isalnum(*str)) {
58
+ hash_val = 5*hash_val + tolower(*str);
59
+ }
60
+ str++;
61
+ }
62
+ return (size_t)hash_val;
63
+ }
64
+ };
65
+
66
+ struct CStringAlnumCaseEqual {
67
+ bool operator()(const char *str1, const char *str2) const {
68
+ while (true) {
69
+ // Skip until each pointer is pointing to an alphanumeric char or '\0'
70
+ while (!isalnum(*str1) && (*str1 != '\0')) {
71
+ str1++;
72
+ }
73
+ while (!isalnum(*str2) && (*str2 != '\0')) {
74
+ str2++;
75
+ }
76
+ if (tolower(*str1) != tolower(*str2)) {
77
+ return false; // mismatch on alphanumeric char or '\0'
78
+ }
79
+ if (*str1 == '\0') { // in which case *str2 must be '\0' as well
80
+ return true; // reached '\0' in both strings without mismatch
81
+ }
82
+ str1++;
83
+ str2++;
84
+ }
85
+ }
86
+ };
87
+
88
+ #endif // UTIL_CASE_INSENSITIVE_HASH_H_
@@ -0,0 +1,39 @@
1
+ // Copyright 2016 Google Inc.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ //
15
+ ////////////////////////////////////////////////////////////////////////////////
16
+
17
+ #ifndef UTIL_COMMANDLINEFLAGS_H_
18
+ #define UTIL_COMMANDLINEFLAGS_H_
19
+
20
+
21
+ #undef DEFINE_bool
22
+ #define DEFINE_bool(name, default_value, comment) \
23
+ bool FLAGS_##name = default_value
24
+ #undef DEFINE_int32
25
+ #define DEFINE_int32(name, default_value, comment) \
26
+ int32 FLAGS_##name = default_value
27
+ #undef DEFINE_string
28
+ #define DEFINE_string(name, default_value, comment) \
29
+ string FLAGS_##name = default_value
30
+
31
+ #undef DECLARE_bool
32
+ #define DECLARE_bool(name) extern bool FLAGS_##name
33
+ #undef DECLARE_int32
34
+ #define DECLARE_int32(name) extern int32 FLAGS_##name
35
+ #undef DECLARE_string
36
+ #define DECLARE_string(name) extern string FLAGS_##name
37
+
38
+
39
+ #endif // UTIL_COMMANDLINEFLAGS_H_