18a525b5d9678c2439d496703e7837226d2023304856de37a66e13b2d8e863ce5a0959daefd6c43b64c069f9770365adb7e3e4a73a77cb48bb365c5a73d974 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { toUint8 } from '../../../base/common/uint.js';
  6. /**
  7. * A fast character classifier that uses a compact array for ASCII values.
  8. */
  9. export class CharacterClassifier {
  10. constructor(_defaultValue) {
  11. const defaultValue = toUint8(_defaultValue);
  12. this._defaultValue = defaultValue;
  13. this._asciiMap = CharacterClassifier._createAsciiMap(defaultValue);
  14. this._map = new Map();
  15. }
  16. static _createAsciiMap(defaultValue) {
  17. const asciiMap = new Uint8Array(256);
  18. for (let i = 0; i < 256; i++) {
  19. asciiMap[i] = defaultValue;
  20. }
  21. return asciiMap;
  22. }
  23. set(charCode, _value) {
  24. const value = toUint8(_value);
  25. if (charCode >= 0 && charCode < 256) {
  26. this._asciiMap[charCode] = value;
  27. }
  28. else {
  29. this._map.set(charCode, value);
  30. }
  31. }
  32. get(charCode) {
  33. if (charCode >= 0 && charCode < 256) {
  34. return this._asciiMap[charCode];
  35. }
  36. else {
  37. return (this._map.get(charCode) || this._defaultValue);
  38. }
  39. }
  40. }
  41. export class CharacterSet {
  42. constructor() {
  43. this._actual = new CharacterClassifier(0 /* Boolean.False */);
  44. }
  45. add(charCode) {
  46. this._actual.set(charCode, 1 /* Boolean.True */);
  47. }
  48. has(charCode) {
  49. return (this._actual.get(charCode) === 1 /* Boolean.True */);
  50. }
  51. }