27
27
use std:: ops:: Range ;
28
28
29
29
mod bitrank;
30
-
31
30
use bitrank:: { BitRank , BitRankBuilder } ;
32
31
32
+ #[ cfg( feature = "wasm" ) ]
33
33
use wasm_bindgen:: prelude:: * ;
34
34
35
35
/// 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::*;
86
86
/// Most operations run in O(1) time. A few require O(log n) time. The memory consumed by this
87
87
/// data structure is typically less than the memory occupied by the actual content. In the best
88
88
/// case, it requires ~45% of the content space.
89
- #[ wasm_bindgen]
89
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen) ]
90
90
pub struct StringOffsets {
91
91
/// Vector storing, for every line, the byte position at which the line starts.
92
92
line_begins : Vec < u32 > ,
@@ -108,7 +108,7 @@ pub struct StringOffsets {
108
108
}
109
109
110
110
/// A position in a string, specified by line and column number.
111
- #[ wasm_bindgen]
111
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen) ]
112
112
#[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
113
113
pub struct Pos {
114
114
/// Zero-indexed line number.
@@ -136,10 +136,10 @@ pub struct Pos {
136
136
// Question: Consider whether we should return an empty line range in this case which would
137
137
// probably be consistent from a mathematical point of view. But then we should also return empty
138
138
// line ranges for empty character ranges in the middle of a line...
139
- #[ wasm_bindgen]
139
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen) ]
140
140
impl StringOffsets {
141
141
/// Create a new converter to work with offsets into the given string.
142
- #[ wasm_bindgen( constructor) ]
142
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( constructor) ) ]
143
143
pub fn new ( content : & str ) -> Self {
144
144
new_converter ( content. as_bytes ( ) )
145
145
}
@@ -149,26 +149,26 @@ impl StringOffsets {
149
149
/// If `content` is UTF-8, this is just like [`StringOffsets::new`]. Otherwise, the
150
150
/// conversion methods will produce unspecified (but memory-safe) results.
151
151
#[ allow( unused_variables) ]
152
- #[ wasm_bindgen( static_method_of = StringOffsets ) ]
152
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( static_method_of = StringOffsets ) ) ]
153
153
pub fn from_bytes ( content : & [ u8 ] ) -> Self {
154
154
new_converter ( content)
155
155
}
156
156
157
157
/// 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) ) ]
159
159
pub fn line_chars ( & self , line_number : usize ) -> usize {
160
160
let r = self . utf8s_to_chars ( self . line_to_utf8s ( line_number) ) ;
161
161
r. end - r. start
162
162
}
163
163
164
164
/// Returns the number of lines in the string.
165
- #[ wasm_bindgen( js_name = lines) ]
165
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = lines) ) ]
166
166
pub fn lines ( & self ) -> usize {
167
167
self . line_begins . len ( ) - 1
168
168
}
169
169
170
170
/// 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) ) ]
172
172
pub fn only_whitespaces ( & self , line_number : usize ) -> bool {
173
173
self . whitespace_only
174
174
. get ( line_number)
@@ -180,7 +180,7 @@ impl StringOffsets {
180
180
///
181
181
/// If `line_number` is greater than or equal to the number of lines in the text, this returns
182
182
/// the length of the string.
183
- #[ wasm_bindgen( js_name = lineToUtf8Begin) ]
183
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = lineToUtf8Begin) ) ]
184
184
pub fn line_to_utf8_begin ( & self , line_number : usize ) -> usize {
185
185
self . line_begins [ line_number. min ( self . lines ( ) ) ] as usize
186
186
}
@@ -189,7 +189,7 @@ impl StringOffsets {
189
189
///
190
190
/// That is, return the offset that would point to the start of that line in a UTF-16
191
191
/// representation of the source string.
192
- #[ wasm_bindgen( js_name = lineToUtf16Begin) ]
192
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = lineToUtf16Begin) ) ]
193
193
pub fn line_to_utf16_begin ( & self , line_number : usize ) -> usize {
194
194
self . utf8_to_utf16 ( self . line_to_utf8_begin ( line_number) )
195
195
}
@@ -198,39 +198,39 @@ impl StringOffsets {
198
198
///
199
199
/// That is, return the offset that would point to the start of that line in a UTF-32
200
200
/// representation of the source string.
201
- #[ wasm_bindgen( js_name = lineToCharBegin) ]
201
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = lineToCharBegin) ) ]
202
202
pub fn line_to_char_begin ( & self , line_number : usize ) -> usize {
203
203
self . utf8_to_char ( self . line_to_utf8_begin ( line_number) )
204
204
}
205
205
206
206
/// 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) ) ]
208
208
pub fn line_to_utf8_end ( & self , line_number : usize ) -> usize {
209
209
self . line_to_utf8_begin ( line_number + 1 )
210
210
}
211
211
212
212
/// 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) ) ]
214
214
pub fn line_to_utf16_end ( & self , line_number : usize ) -> usize {
215
215
self . utf8_to_utf16 ( self . line_to_utf8_end ( line_number) )
216
216
}
217
217
218
218
/// 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) ) ]
220
220
pub fn line_to_char_end ( & self , line_number : usize ) -> usize {
221
221
self . utf8_to_char ( self . line_to_utf8_end ( line_number) )
222
222
}
223
223
224
224
/// Return the zero-based line number of the line containing the specified UTF-8 offset.
225
225
/// 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) ) ]
227
227
pub fn utf8_to_line ( & self , byte_number : usize ) -> usize {
228
228
self . utf8_to_line . rank ( byte_number)
229
229
}
230
230
231
231
/// Converts a UTF-8 offset to a zero-based line number and UTF-32 offset within the
232
232
/// line.
233
- #[ wasm_bindgen( js_name = utf8ToCharPos) ]
233
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = utf8ToCharPos) ) ]
234
234
pub fn utf8_to_char_pos ( & self , byte_number : usize ) -> Pos {
235
235
let line = self . utf8_to_line ( byte_number) ;
236
236
let line_start_char_number = self . line_to_char_begin ( line) ;
@@ -243,7 +243,7 @@ impl StringOffsets {
243
243
244
244
/// Converts a UTF-8 offset to a zero-based line number and UTF-16 offset within the
245
245
/// line.
246
- #[ wasm_bindgen( js_name = utf8ToUtf16Pos) ]
246
+ #[ cfg_attr ( feature = "wasm" , wasm_bindgen( js_name = utf8ToUtf16Pos) ) ]
247
247
pub fn utf8_to_utf16_pos ( & self , byte_number : usize ) -> Pos {
248
248
let line = self . utf8_to_line ( byte_number) ;
249
249
let line_start_char_number = self . line_to_utf16_begin ( line) ;
@@ -255,19 +255,19 @@ impl StringOffsets {
255
255
}
256
256
257
257
/// 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) ) ]
259
259
pub fn utf8_to_char ( & self , byte_number : usize ) -> usize {
260
260
self . utf8_to_char . rank ( byte_number)
261
261
}
262
262
263
263
/// 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) ) ]
265
265
pub fn utf8_to_utf16 ( & self , byte_number : usize ) -> usize {
266
266
self . utf8_to_utf16 . rank ( byte_number)
267
267
}
268
268
269
269
/// 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) ) ]
271
271
pub fn char_to_utf8 ( & self , char_number : usize ) -> usize {
272
272
let mut byte_number = char_number;
273
273
for _ in 0 ..128 {
0 commit comments