Debian Codegraph @

closure-library/closure/goog/i18n/charlistdecompressor.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
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 * @fileoverview The decompressor for Base88 compressed character lists.
17 *
18 * The compression is by base 88 encoding the delta between two adjacent
19 * characters in ths list. The deltas can be positive or negative. Also, there
20 * would be character ranges. These three types of values
21 * are given enum values 0, 1 and 2 respectively. Initial 3 bits are used for
22 * encoding the type and total length of the encoded value. Length enums 0, 1
23 * and 2 represents lengths 1, 2 and 4. So (value * 8 + type * 3 + length enum)
24 * is encoded in base 88 by following characters for numbers from 0 to 87:
25 * 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ (continued in next line)
26 * abcdefghijklmnopqrstuvwxyz!#$%()*+,-.:;<=>?@[]^_`{|}~
27 *
28 * Value uses 0 based counting. That is value for the range [a, b] is 0 and
29 * that of [a, c] is 1. Simillarly, the delta of "ab" is 0.
30 *
31 * Following python script can be used to compress character lists taken
32 * standard input: http://go/charlistcompressor.py
33 *
34 */
35
36 goog.provide('goog.i18n.CharListDecompressor');
37
38 goog.require('goog.array');
39 goog.require('goog.i18n.uChar');
40
41
42
43 /**
44 * Class to decompress base88 compressed character list.
45 * @constructor
46 * @final
47 */
48 goog.i18n.CharListDecompressor = function() {
49 this.buildCharMap_(
50 '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr' +
51 'stuvwxyz!#$%()*+,-.:;<=>?@[]^_`{|}~');
52 };
53
54
55 /**
56 * 1-1 mapping from ascii characters used in encoding to an integer in the
57 * range 0 to 87.
58 * @type {Object}
59 * @private
60 */
61 goog.i18n.CharListDecompressor.prototype.charMap_ = null;
62
63
64 /**
65 * Builds the map from ascii characters used for the base88 scheme to number
66 * each character represents.
67 * @param {string} str The string of characters used in base88 scheme.
68 * @private
69 */
70 goog.i18n.CharListDecompressor.prototype.buildCharMap_ = function(str) {
71 if (!this.charMap_) {
72 this.charMap_ = {};
73 for (var i = 0; i < str.length; i++) {
74 this.charMap_[str.charAt(i)] = i;
75 }
76 }
77 };
78
79
80 /**
81 * Gets the number encoded in base88 scheme by a substring of given length
82 * and placed at the a given position of the string.
83 * @param {string} str String containing sequence of characters encoding a
84 * number in base 88 scheme.
85 * @param {number} start Starting position of substring encoding the number.
86 * @param {number} leng Length of the substring encoding the number.
87 * @return {number} The encoded number.
88 * @private
89 */
90 goog.i18n.CharListDecompressor.prototype.getCodeAt_ = function(
91 str, start, leng) {
92 var result = 0;
93 for (var i = 0; i < leng; i++) {
94 var c = this.charMap_[str.charAt(start + i)];
95 result += c * Math.pow(88, i);
96 }
97 return result;
98 };
99
100
101 /**
102 * Add character(s) specified by the value and type to given list and return
103 * the next character in the sequence.
104 * @param {Array<string>} list The list of characters to which the specified
105 * characters are appended.
106 * @param {number} lastcode The last codepoint that was added to the list.
107 * @param {number} value The value component that representing the delta or
108 * range.
109 * @param {number} type The type component that representing whether the value
110 * is a positive or negative delta or range.
111 * @return {number} Last codepoint that is added to the list.
112 * @private
113 */
114 goog.i18n.CharListDecompressor.prototype.addChars_ = function(
115 list, lastcode, value, type) {
116 if (type == 0) {
117 lastcode += value + 1;
118 goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
119 } else if (type == 1) {
120 lastcode -= value + 1;
121 goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
122 } else if (type == 2) {
123 for (var i = 0; i <= value; i++) {
124 lastcode++;
125 goog.array.extend(list, goog.i18n.uChar.fromCharCode(lastcode));
126 }
127 }
128 return lastcode;
129 };
130
131
132 /**
133 * Gets the list of characters specified in the given string by base 88 scheme.
134 * @param {string} str The string encoding character list.
135 * @return {!Array<string>} The list of characters specified by the given
136 * string in base 88 scheme.
137 */
138 goog.i18n.CharListDecompressor.prototype.toCharList = function(str) {
139 var metasize = 8;
140 var result = [];
141 var lastcode = 0;
142 var i = 0;
143 while (i < str.length) {
144 var c = this.charMap_[str.charAt(i)];
145 var meta = c % metasize;
146 var type = Math.floor(meta / 3);
147 var leng = (meta % 3) + 1;
148 if (leng == 3) {
149 leng++;
150 }
151 var code = this.getCodeAt_(str, i, leng);
152 var value = Math.floor(code / metasize);
153 lastcode = this.addChars_(result, lastcode, value, type);
154
155 i += leng;
156 }
157 return result;
158 };