Skip to content

Commit 83c5fac

Browse files
committed
Add wasm feature to build and make wasm-bindgen optional
1 parent c94f4e6 commit 83c5fac

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

crates/string-offsets/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ exclude = ["/js"]
1313
[lib]
1414
crate-type = ["cdylib", "rlib"]
1515

16+
[features]
17+
wasm = ["wasm-bindgen"]
18+
1619
[dependencies]
17-
wasm-bindgen = "0.2"
20+
wasm-bindgen = { version = "0.2", optional = true }
1821

1922
[dev-dependencies]
2023
rand = "0.9"

crates/string-offsets/js/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
"require": "./pkg/nodejs/string_offsets.js"
2121
},
2222
"scripts": {
23-
"compile:bundler": "wasm-pack build --target bundler -d js/pkg/bundler && node -e \"fs.unlinkSync('./pkg/bundler/.gitignore')\"",
24-
"compile:web": "wasm-pack build --target web -d js/pkg/web && node -e \"fs.unlinkSync('./pkg/web/.gitignore')\"",
25-
"compile:nodejs": "wasm-pack build --target nodejs -d js/pkg/nodejs && node -e \"fs.unlinkSync('./pkg/nodejs/.gitignore')\"",
23+
"compile:bundler": "wasm-pack build --target bundler -d js/pkg/bundler --features wasm && node -e \"fs.unlinkSync('./pkg/bundler/.gitignore')\"",
24+
"compile:web": "wasm-pack build --target web -d js/pkg/web --features wasm && node -e \"fs.unlinkSync('./pkg/web/.gitignore')\"",
25+
"compile:nodejs": "wasm-pack build --target nodejs -d js/pkg/nodejs --features wasm && node -e \"fs.unlinkSync('./pkg/nodejs/.gitignore')\"",
2626
"compile": "npm run compile:web && npm run compile:bundler && npm run compile:nodejs",
2727
"test": "jest"
2828
},

crates/string-offsets/src/lib.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
use std::ops::Range;
2828

2929
mod bitrank;
30-
3130
use bitrank::{BitRank, BitRankBuilder};
3231

32+
#[cfg(feature = "wasm")]
3333
use wasm_bindgen::prelude::*;
3434

3535
/// Converts positions within a given string between UTF-8 byte offsets (the usual in Rust), UTF-16
@@ -86,7 +86,7 @@ use wasm_bindgen::prelude::*;
8686
/// Most operations run in O(1) time. A few require O(log n) time. The memory consumed by this
8787
/// data structure is typically less than the memory occupied by the actual content. In the best
8888
/// case, it requires ~45% of the content space.
89-
#[wasm_bindgen]
89+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
9090
pub struct StringOffsets {
9191
/// Vector storing, for every line, the byte position at which the line starts.
9292
line_begins: Vec<u32>,
@@ -108,7 +108,7 @@ pub struct StringOffsets {
108108
}
109109

110110
/// A position in a string, specified by line and column number.
111-
#[wasm_bindgen]
111+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
112112
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
113113
pub struct Pos {
114114
/// Zero-indexed line number.
@@ -136,10 +136,10 @@ pub struct Pos {
136136
// Question: Consider whether we should return an empty line range in this case which would
137137
// probably be consistent from a mathematical point of view. But then we should also return empty
138138
// line ranges for empty character ranges in the middle of a line...
139-
#[wasm_bindgen]
139+
#[cfg_attr(feature = "wasm", wasm_bindgen)]
140140
impl StringOffsets {
141141
/// Create a new converter to work with offsets into the given string.
142-
#[wasm_bindgen(constructor)]
142+
#[cfg_attr(feature = "wasm", wasm_bindgen(constructor))]
143143
pub fn new(content: &str) -> Self {
144144
new_converter(content.as_bytes())
145145
}
@@ -149,26 +149,26 @@ impl StringOffsets {
149149
/// If `content` is UTF-8, this is just like [`StringOffsets::new`]. Otherwise, the
150150
/// conversion methods will produce unspecified (but memory-safe) results.
151151
#[allow(unused_variables)]
152-
#[wasm_bindgen(static_method_of = StringOffsets)]
152+
#[cfg_attr(feature = "wasm", wasm_bindgen(static_method_of = StringOffsets))]
153153
pub fn from_bytes(content: &[u8]) -> Self {
154154
new_converter(content)
155155
}
156156

157157
/// Returns the number of Unicode characters on the specified line.
158-
#[wasm_bindgen(js_name = lineChars)]
158+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineChars))]
159159
pub fn line_chars(&self, line_number: usize) -> usize {
160160
let r = self.utf8s_to_chars(self.line_to_utf8s(line_number));
161161
r.end - r.start
162162
}
163163

164164
/// Returns the number of lines in the string.
165-
#[wasm_bindgen(js_name = lines)]
165+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lines))]
166166
pub fn lines(&self) -> usize {
167167
self.line_begins.len() - 1
168168
}
169169

170170
/// Returns true if the specified line is empty except for whitespace.
171-
#[wasm_bindgen(js_name = onlyWhitespaces)]
171+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = onlyWhitespaces))]
172172
pub fn only_whitespaces(&self, line_number: usize) -> bool {
173173
self.whitespace_only
174174
.get(line_number)
@@ -180,7 +180,7 @@ impl StringOffsets {
180180
///
181181
/// If `line_number` is greater than or equal to the number of lines in the text, this returns
182182
/// the length of the string.
183-
#[wasm_bindgen(js_name = lineToUtf8Begin)]
183+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToUtf8Begin))]
184184
pub fn line_to_utf8_begin(&self, line_number: usize) -> usize {
185185
self.line_begins[line_number.min(self.lines())] as usize
186186
}
@@ -189,7 +189,7 @@ impl StringOffsets {
189189
///
190190
/// That is, return the offset that would point to the start of that line in a UTF-16
191191
/// representation of the source string.
192-
#[wasm_bindgen(js_name = lineToUtf16Begin)]
192+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToUtf16Begin))]
193193
pub fn line_to_utf16_begin(&self, line_number: usize) -> usize {
194194
self.utf8_to_utf16(self.line_to_utf8_begin(line_number))
195195
}
@@ -198,39 +198,39 @@ impl StringOffsets {
198198
///
199199
/// That is, return the offset that would point to the start of that line in a UTF-32
200200
/// representation of the source string.
201-
#[wasm_bindgen(js_name = lineToCharBegin)]
201+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToCharBegin))]
202202
pub fn line_to_char_begin(&self, line_number: usize) -> usize {
203203
self.utf8_to_char(self.line_to_utf8_begin(line_number))
204204
}
205205

206206
/// UTF-8 offset of the first character of a line.
207-
#[wasm_bindgen(js_name = lineToUtf8End)]
207+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToUtf8End))]
208208
pub fn line_to_utf8_end(&self, line_number: usize) -> usize {
209209
self.line_to_utf8_begin(line_number + 1)
210210
}
211211

212212
/// 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)]
213+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToUtf16End))]
214214
pub fn line_to_utf16_end(&self, line_number: usize) -> usize {
215215
self.utf8_to_utf16(self.line_to_utf8_end(line_number))
216216
}
217217

218218
/// 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)]
219+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = lineToCharEnd))]
220220
pub fn line_to_char_end(&self, line_number: usize) -> usize {
221221
self.utf8_to_char(self.line_to_utf8_end(line_number))
222222
}
223223

224224
/// Return the zero-based line number of the line containing the specified UTF-8 offset.
225225
/// Newline characters count as part of the preceding line.
226-
#[wasm_bindgen(js_name = utf8ToLine)]
226+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = utf8ToLine))]
227227
pub fn utf8_to_line(&self, byte_number: usize) -> usize {
228228
self.utf8_to_line.rank(byte_number)
229229
}
230230

231231
/// Converts a UTF-8 offset to a zero-based line number and UTF-32 offset within the
232232
/// line.
233-
#[wasm_bindgen(js_name = utf8ToCharPos)]
233+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = utf8ToCharPos))]
234234
pub fn utf8_to_char_pos(&self, byte_number: usize) -> Pos {
235235
let line = self.utf8_to_line(byte_number);
236236
let line_start_char_number = self.line_to_char_begin(line);
@@ -243,7 +243,7 @@ impl StringOffsets {
243243

244244
/// Converts a UTF-8 offset to a zero-based line number and UTF-16 offset within the
245245
/// line.
246-
#[wasm_bindgen(js_name = utf8ToUtf16Pos)]
246+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = utf8ToUtf16Pos))]
247247
pub fn utf8_to_utf16_pos(&self, byte_number: usize) -> Pos {
248248
let line = self.utf8_to_line(byte_number);
249249
let line_start_char_number = self.line_to_utf16_begin(line);
@@ -255,19 +255,19 @@ impl StringOffsets {
255255
}
256256

257257
/// Converts a UTF-8 offset to a UTF-32 offset.
258-
#[wasm_bindgen(js_name = utf8ToChar)]
258+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = utf8ToChar))]
259259
pub fn utf8_to_char(&self, byte_number: usize) -> usize {
260260
self.utf8_to_char.rank(byte_number)
261261
}
262262

263263
/// Converts a UTF-8 offset to a UTF-16 offset.
264-
#[wasm_bindgen(js_name = utf8ToUtf16)]
264+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = utf8ToUtf16))]
265265
pub fn utf8_to_utf16(&self, byte_number: usize) -> usize {
266266
self.utf8_to_utf16.rank(byte_number)
267267
}
268268

269269
/// Converts a UTF-32 offset to a UTF-8 offset.
270-
#[wasm_bindgen(js_name = charToUtf8)]
270+
#[cfg_attr(feature = "wasm", wasm_bindgen(js_name = charToUtf8))]
271271
pub fn char_to_utf8(&self, char_number: usize) -> usize {
272272
let mut byte_number = char_number;
273273
for _ in 0..128 {

0 commit comments

Comments
 (0)