Skip to content

Commit c4b9c9f

Browse files
committed
Use better js names and add ending newlines
1 parent d01e2d3 commit c4b9c9f

7 files changed

Lines changed: 25 additions & 10 deletions

File tree

crates/string-offsets/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
22
pkg
3-
.tgz
3+
.tgz

crates/string-offsets/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ wasm-bindgen = "0.2"
1818

1919
[dev-dependencies]
2020
rand = "0.9"
21-
rand_chacha = "0.9"
21+
rand_chacha = "0.9"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
/*
22
!/pkg
33
pkg/*/package.json
4-
pkg/*/README.md
4+
pkg/*/README.md

crates/string-offsets/js/package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/string-offsets/js/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
"jest": "^29.0.0",
3232
"wasm-pack": "^0.13.1"
3333
}
34-
}
34+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//@ts-check
22
const { StringOffsets } = require('../');
3-
3+
44
describe('StringOffsets sanity checks', () => {
55
test('basic ASCII text', () => {
66
const text = "hello\nworld";
77
const offsets = new StringOffsets(text);
88

99
expect(offsets.lines()).toBe(2);
10-
expect(offsets.utf8_to_utf16(0)).toBe(0);
11-
expect(offsets.utf8_to_line(0)).toBe(0);
10+
expect(offsets.utf8ToUtf16(0)).toBe(0);
11+
expect(offsets.utf8ToLine(0)).toBe(0);
1212
});
1313

1414
test('Unicode text', () => {
@@ -17,7 +17,7 @@ describe('StringOffsets sanity checks', () => {
1717

1818
expect(offsets.lines()).toBe(2);
1919
// ☀️ is 6 UTF-8 bytes and 3 UTF-16 code units
20-
expect(offsets.utf8_to_utf16(6)).toBe(2);
21-
expect(offsets.utf8_to_utf16(0)).toBe(0);
20+
expect(offsets.utf8ToUtf16(6)).toBe(2);
21+
expect(offsets.utf8ToUtf16(0)).toBe(0);
2222
});
2323
});

crates/string-offsets/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,17 +155,20 @@ impl StringOffsets {
155155
}
156156

157157
/// Returns the number of Unicode characters on the specified line.
158+
#[wasm_bindgen(js_name = lineChars)]
158159
pub fn line_chars(&self, line_number: usize) -> usize {
159160
let r = self.utf8s_to_chars(self.line_to_utf8s(line_number));
160161
r.end - r.start
161162
}
162163

163164
/// Returns the number of lines in the string.
165+
#[wasm_bindgen(js_name = lines)]
164166
pub fn lines(&self) -> usize {
165167
self.line_begins.len() - 1
166168
}
167169

168170
/// Returns true if the specified line is empty except for whitespace.
171+
#[wasm_bindgen(js_name = onlyWhitespaces)]
169172
pub fn only_whitespaces(&self, line_number: usize) -> bool {
170173
self.whitespace_only
171174
.get(line_number)
@@ -177,6 +180,7 @@ impl StringOffsets {
177180
///
178181
/// If `line_number` is greater than or equal to the number of lines in the text, this returns
179182
/// the length of the string.
183+
#[wasm_bindgen(js_name = lineToUtf8Begin)]
180184
pub fn line_to_utf8_begin(&self, line_number: usize) -> usize {
181185
self.line_begins[line_number.min(self.lines())] as usize
182186
}
@@ -185,6 +189,7 @@ impl StringOffsets {
185189
///
186190
/// That is, return the offset that would point to the start of that line in a UTF-16
187191
/// representation of the source string.
192+
#[wasm_bindgen(js_name = lineToUtf16Begin)]
188193
pub fn line_to_utf16_begin(&self, line_number: usize) -> usize {
189194
self.utf8_to_utf16(self.line_to_utf8_begin(line_number))
190195
}
@@ -193,33 +198,39 @@ impl StringOffsets {
193198
///
194199
/// That is, return the offset that would point to the start of that line in a UTF-32
195200
/// representation of the source string.
201+
#[wasm_bindgen(js_name = lineToCharBegin)]
196202
pub fn line_to_char_begin(&self, line_number: usize) -> usize {
197203
self.utf8_to_char(self.line_to_utf8_begin(line_number))
198204
}
199205

200206
/// UTF-8 offset of the first character of a line.
207+
#[wasm_bindgen(js_name = lineToUtf8End)]
201208
pub fn line_to_utf8_end(&self, line_number: usize) -> usize {
202209
self.line_to_utf8_begin(line_number + 1)
203210
}
204211

205212
/// UTF-16 offset one past the end of a line (the offset of the start of the next line).
213+
#[wasm_bindgen(js_name = lineToUtf16End)]
206214
pub fn line_to_utf16_end(&self, line_number: usize) -> usize {
207215
self.utf8_to_utf16(self.line_to_utf8_end(line_number))
208216
}
209217

210218
/// UTF-32 offset one past the end of a line (the offset of the start of the next line).
219+
#[wasm_bindgen(js_name = lineToCharEnd)]
211220
pub fn line_to_char_end(&self, line_number: usize) -> usize {
212221
self.utf8_to_char(self.line_to_utf8_end(line_number))
213222
}
214223

215224
/// Return the zero-based line number of the line containing the specified UTF-8 offset.
216225
/// Newline characters count as part of the preceding line.
226+
#[wasm_bindgen(js_name = utf8ToLine)]
217227
pub fn utf8_to_line(&self, byte_number: usize) -> usize {
218228
self.utf8_to_line.rank(byte_number)
219229
}
220230

221231
/// Converts a UTF-8 offset to a zero-based line number and UTF-32 offset within the
222232
/// line.
233+
#[wasm_bindgen(js_name = utf8ToCharPos)]
223234
pub fn utf8_to_char_pos(&self, byte_number: usize) -> Pos {
224235
let line = self.utf8_to_line(byte_number);
225236
let line_start_char_number = self.line_to_char_begin(line);
@@ -232,6 +243,7 @@ impl StringOffsets {
232243

233244
/// Converts a UTF-8 offset to a zero-based line number and UTF-16 offset within the
234245
/// line.
246+
#[wasm_bindgen(js_name = utf8ToUtf16Pos)]
235247
pub fn utf8_to_utf16_pos(&self, byte_number: usize) -> Pos {
236248
let line = self.utf8_to_line(byte_number);
237249
let line_start_char_number = self.line_to_utf16_begin(line);
@@ -243,16 +255,19 @@ impl StringOffsets {
243255
}
244256

245257
/// Converts a UTF-8 offset to a UTF-32 offset.
258+
#[wasm_bindgen(js_name = utf8ToChar)]
246259
pub fn utf8_to_char(&self, byte_number: usize) -> usize {
247260
self.utf8_to_char.rank(byte_number)
248261
}
249262

250263
/// Converts a UTF-8 offset to a UTF-16 offset.
264+
#[wasm_bindgen(js_name = utf8ToUtf16)]
251265
pub fn utf8_to_utf16(&self, byte_number: usize) -> usize {
252266
self.utf8_to_utf16.rank(byte_number)
253267
}
254268

255269
/// Converts a UTF-32 offset to a UTF-8 offset.
270+
#[wasm_bindgen(js_name = charToUtf8)]
256271
pub fn char_to_utf8(&self, char_number: usize) -> usize {
257272
let mut byte_number = char_number;
258273
for _ in 0..128 {

0 commit comments

Comments
 (0)