Skip to content

Commit

Permalink
refactor: grapheme count fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-ext committed Dec 9, 2023
1 parent 4269209 commit c34bbc3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
6 changes: 3 additions & 3 deletions app/api/richtext/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { XRPCError } from '@externdefs/bluesky-client/xrpc-utils';

import { multiagent } from '../globals/agent.ts';

import { graphemeLen } from './intl.ts';
import { asciiLen, graphemeLen } from './intl.ts';
import { toShortUrl } from './renderer.ts';

import type { Facet, LinkFeature, MentionFeature, TagFeature, UnresolvedMentionFeature } from './types.ts';
Expand Down Expand Up @@ -183,13 +183,13 @@ export const getRtText = (rt: PreliminaryRichText) => {

export const getRtLength = (rt: PreliminaryRichText) => {
const text = getRtText(rt);
return isAscii(text) ? text.length : graphemeLen(text);
return graphemeLen(text);
};

const encoder = new TextEncoder();

const getUtf8Length = (str: string): number => {
return isAscii(str) ? str.length : encoder.encode(str).byteLength;
return asciiLen(str) ?? encoder.encode(str).byteLength;
};

export const finalizeRt = async (uid: DID, rt: PreliminaryRichText) => {
Expand Down
36 changes: 29 additions & 7 deletions app/api/richtext/intl.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
export let graphemeLen: (text: string) => number;
var _graphemeLen: (text: string) => number;

export const graphemeLen = (text: string) => {
var length = asciiLen(text);

if (length === undefined) {
return _graphemeLen(text);
}

return length;
};

export const asciiLen = (str: string) => {
for (var idx = 0, len = str.length; idx < len; idx++) {
const char = str.charCodeAt(idx);

if (char > 127) {
return undefined;
}
}

return len;
};

if (Intl.Segmenter) {
const segmenter = new Intl.Segmenter();
var segmenter = new Intl.Segmenter();

graphemeLen = (text) => {
const iterator = segmenter.segment(text)[Symbol.iterator]();
let count = 0;
_graphemeLen = (text) => {
var iterator = segmenter.segment(text)[Symbol.iterator]();
var count = 0;

while (!iterator.next().done) {
count++;
Expand All @@ -16,6 +38,6 @@ if (Intl.Segmenter) {
} else {
console.log('Intl.Segmenter API not available, falling back to polyfill...');

const { countGraphemes } = await import('./graphemer.ts');
graphemeLen = countGraphemes;
var { countGraphemes } = await import('./graphemer.ts');
_graphemeLen = countGraphemes;
}

0 comments on commit c34bbc3

Please sign in to comment.