Skip to content

Commit

Permalink
perf(string): better slice by bytes
Browse files Browse the repository at this point in the history
Avoid create Buffer, use character index.
  • Loading branch information
chemzqm committed Oct 24, 2022
1 parent 3b0bbda commit be402d8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/modules/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,12 @@ describe('strings', () => {
expect(strings.utf8_code2len(65537)).toBe(4)
})

it('should slice content by bytes', async () => {
expect(strings.byteSlice('你', 0, 1)).toBe('你')
expect(strings.byteSlice('你', 0, 3)).toBe('你')
expect(strings.byteSlice('abc你', 3, 6)).toBe('你')
})

it('should get case', async () => {
expect(strings.getCase('a'.charCodeAt(0))).toBe(1)
expect(strings.getCase('A'.charCodeAt(0))).toBe(2)
Expand Down
2 changes: 1 addition & 1 deletion src/completion/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class Completion implements Disposable {
option.filetype = doc.filetype
logger.debug('trigger completion with', option)
this.stop(true)
this.pretext = option.line.slice(0, characterIndex(option.line, option.colnr - 1))
this.pretext = byteSlice(option.line, 0, option.colnr - 1)
sourceList = sourceList ?? getSources(option)
if (isFalsyOrEmpty(sourceList)) return
let complete = this.complete = new Complete(
Expand Down
17 changes: 6 additions & 11 deletions src/util/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,20 @@ export function characterIndex(content: string, byteIndex: number): number {
return characterIndex
}

/**
* No need to create Buffer
*/
export function utf8ByteSlice(content: string, start: number, end: number) {
let si = characterIndex(content, start)
let ei = characterIndex(content, end)
return content.slice(si, ei)
}

export function utf8_code2len(code: number): number {
if (code < UTF8_2BYTES_START) return 1
if (code < UTF8_3BYTES_START) return 2
if (code < UTF8_4BYTES_START) return 3
return 4
}

/**
* No need to create Buffer
*/
export function byteSlice(content: string, start: number, end?: number): string {
let buf = Buffer.from(content, 'utf8')
return buf.slice(start, end).toString('utf8')
let si = characterIndex(content, start)
let ei = characterIndex(content, end)
return content.slice(si, ei)
}

export function isWord(character: string): boolean {
Expand Down

0 comments on commit be402d8

Please sign in to comment.