export function numColumns()

in src/lib/CodeMirror/indentationMarkers.ts [55:89]


export function numColumns(str: string, tabSize: number): number {
	// as far as I can tell, this is pretty much the fastest way to do this,
	// at least involving iteration. `str.length - str.trimStart().length` is
	// much faster, but it has some edge cases that are hard to deal with.

	let col = 0;

	// eslint-disable-next-line no-restricted-syntax, @typescript-eslint/prefer-for-of
	loop: for (let i = 0; i < str.length; i++) {
		switch (str[i]) {
			case ' ': {
				col += 1;
				continue loop;
			}

			case '\t': {
				// if the current column is a multiple of the tab size, we can just
				// add the tab size to the column. otherwise, we need to add the
				// difference between the tab size and the current column.
				col += tabSize - (col % tabSize);
				continue loop;
			}

			case '\r': {
				continue loop;
			}

			default: {
				break loop;
			}
		}
	}

	return col;
}