Skip to content

Commit

Permalink
fix(chars): fix ranges for keyword chars
Browse files Browse the repository at this point in the history
Always consider A-Z, a-z as keywords
  • Loading branch information
chemzqm committed May 28, 2022
1 parent 7a9b807 commit aede54f
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
7 changes: 0 additions & 7 deletions src/__tests__/modules/chars.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,8 @@ describe('chars keyword option', () => {
expect(chars.isKeywordChar('\u205f')).toBe(false)
})

it('should match letter range', () => {
let chars = new Chars('a-z')
expect(chars.isKeywordChar('a')).toBe(true)
expect(chars.isKeywordChar('A')).toBe(false)
})

it('should match code range', () => {
let chars = new Chars('48-57')
expect(chars.isKeywordChar('a')).toBe(false)
expect(chars.isKeywordChar('0')).toBe(true)
expect(chars.isKeywordChar('9')).toBe(true)
})
Expand Down
10 changes: 3 additions & 7 deletions src/model/chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ export class Range {
public static fromKeywordOption(keywordOption: string): Range[] {
let parts = keywordOption.split(',')
let ranges: Range[] = []
ranges.push(new Range(65, 90))
ranges.push(new Range(97, 122))
for (let part of parts) {
if (part == '@') {
// isalpha() of c
ranges.push(new Range(65, 90))
ranges.push(new Range(97, 122))
ranges.push(new Range(256, 65535))
} else if (part == '@-@') {
ranges.push(new Range(64))
} else if (/^([A-Za-z])-([A-Za-z])$/.test(part)) {
let ms = part.match(/^([A-Za-z])-([A-Za-z])$/)
ranges.push(new Range(ms[1].charCodeAt(0), ms[2].charCodeAt(0)))
} else if (/^\d+-\d+$/.test(part)) {
let ms = part.match(/^(\d+)-(\d+)$/)
ranges.push(new Range(Number(ms[1]), Number(ms[2])))
Expand Down Expand Up @@ -107,7 +104,6 @@ export class Chars {
let { ranges } = this
if (/\s/.test(ch)) return false
let c = ch.charCodeAt(0)
if (c > 255) return true
if (c < 33) return false
return ranges.some(r => r.contains(c))
}
Expand Down

0 comments on commit aede54f

Please sign in to comment.