Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

lz-string-1.0.2.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright © 2013 Pieroxy <[email protected]>
  2. // This work is free. You can redistribute it and/or modify it
  3. // under the terms of the WTFPL, Version 2
  4. // For more information see LICENSE.txt or http://www.wtfpl.net/
  5. //
  6. // LZ-based compression algorithm, version 1.0.2-rc1
  7. var LZString = {
  8. writeBit : function(value, data) {
  9. data.val = (data.val << 1) | value;
  10. if (data.position == 15) {
  11. data.position = 0;
  12. data.string += String.fromCharCode(data.val);
  13. data.val = 0;
  14. } else {
  15. data.position++;
  16. }
  17. },
  18. writeBits : function(numBits, value, data) {
  19. if (typeof(value)=="string")
  20. value = value.charCodeAt(0);
  21. for (var i=0 ; i<numBits ; i++) {
  22. this.writeBit(value&1, data);
  23. value = value >> 1;
  24. }
  25. },
  26. produceW : function (context) {
  27. if (Object.prototype.hasOwnProperty.call(context.dictionaryToCreate,context.w)) {
  28. if (context.w.charCodeAt(0)<256) {
  29. this.writeBits(context.numBits, 0, context.data);
  30. this.writeBits(8, context.w, context.data);
  31. } else {
  32. this.writeBits(context.numBits, 1, context.data);
  33. this.writeBits(16, context.w, context.data);
  34. }
  35. this.decrementEnlargeIn(context);
  36. delete context.dictionaryToCreate[context.w];
  37. } else {
  38. this.writeBits(context.numBits, context.dictionary[context.w], context.data);
  39. }
  40. this.decrementEnlargeIn(context);
  41. },
  42. decrementEnlargeIn : function(context) {
  43. context.enlargeIn--;
  44. if (context.enlargeIn == 0) {
  45. context.enlargeIn = Math.pow(2, context.numBits);
  46. context.numBits++;
  47. }
  48. },
  49. compress: function (uncompressed) {
  50. var context = {
  51. dictionary: {},
  52. dictionaryToCreate: {},
  53. c:"",
  54. wc:"",
  55. w:"",
  56. enlargeIn: 2, // Compensate for the first entry which should not count
  57. dictSize: 3,
  58. numBits: 2,
  59. result: "",
  60. data: {string:"", val:0, position:0}
  61. }, i;
  62. for (i = 0; i < uncompressed.length; i += 1) {
  63. context.c = uncompressed.charAt(i);
  64. if (!Object.prototype.hasOwnProperty.call(context.dictionary,context.c)) {
  65. context.dictionary[context.c] = context.dictSize++;
  66. context.dictionaryToCreate[context.c] = true;
  67. }
  68. context.wc = context.w + context.c;
  69. if (Object.prototype.hasOwnProperty.call(context.dictionary,context.wc)) {
  70. context.w = context.wc;
  71. } else {
  72. this.produceW(context);
  73. // Add wc to the dictionary.
  74. context.dictionary[context.wc] = context.dictSize++;
  75. context.w = String(context.c);
  76. }
  77. }
  78. // Output the code for w.
  79. if (context.w !== "") {
  80. this.produceW(context);
  81. }
  82. // Mark the end of the stream
  83. this.writeBits(context.numBits, 2, context.data);
  84. // Flush the last char
  85. while (context.data.val>0) this.writeBit(0,context.data)
  86. return context.data.string;
  87. },
  88. readBit : function(data) {
  89. var res = data.val & data.position;
  90. data.position >>= 1;
  91. if (data.position == 0) {
  92. data.position = 32768;
  93. data.val = data.string.charCodeAt(data.index++);
  94. }
  95. //data.val = (data.val << 1);
  96. return res>0 ? 1 : 0;
  97. },
  98. readBits : function(numBits, data) {
  99. var res = 0;
  100. var maxpower = Math.pow(2,numBits);
  101. var power=1;
  102. while (power!=maxpower) {
  103. res |= this.readBit(data) * power;
  104. power <<= 1;
  105. }
  106. return res;
  107. },
  108. decompress: function (compressed) {
  109. var dictionary = {},
  110. next,
  111. enlargeIn = 4,
  112. dictSize = 4,
  113. numBits = 3,
  114. entry = "",
  115. result = "",
  116. i,
  117. w,
  118. c,
  119. errorCount=0,
  120. literal,
  121. data = {string:compressed, val:compressed.charCodeAt(0), position:32768, index:1};
  122. for (i = 0; i < 3; i += 1) {
  123. dictionary[i] = i;
  124. }
  125. next = this.readBits(2, data);
  126. switch (next) {
  127. case 0:
  128. c = String.fromCharCode(this.readBits(8, data));
  129. break;
  130. case 1:
  131. c = String.fromCharCode(this.readBits(16, data));
  132. break;
  133. case 2:
  134. return "";
  135. }
  136. dictionary[3] = c;
  137. w = result = c;
  138. while (true) {
  139. c = this.readBits(numBits, data);
  140. switch (c) {
  141. case 0:
  142. if (errorCount++ > 10000) return "Error";
  143. c = String.fromCharCode(this.readBits(8, data));
  144. dictionary[dictSize++] = c;
  145. c = dictSize-1;
  146. enlargeIn--;
  147. break;
  148. case 1:
  149. c = String.fromCharCode(this.readBits(16, data));
  150. dictionary[dictSize++] = c;
  151. c = dictSize-1;
  152. enlargeIn--;
  153. break;
  154. case 2:
  155. return result;
  156. }
  157. if (enlargeIn == 0) {
  158. enlargeIn = Math.pow(2, numBits);
  159. numBits++;
  160. }
  161. if (dictionary[c]) {
  162. entry = dictionary[c];
  163. } else {
  164. if (c === dictSize) {
  165. entry = w + w.charAt(0);
  166. } else {
  167. return null;
  168. }
  169. }
  170. result += entry;
  171. // Add w+entry[0] to the dictionary.
  172. dictionary[dictSize++] = w + entry.charAt(0);
  173. enlargeIn--;
  174. w = entry;
  175. if (enlargeIn == 0) {
  176. enlargeIn = Math.pow(2, numBits);
  177. numBits++;
  178. }
  179. }
  180. return result;
  181. }
  182. };