Skip to content

Commit c6d1deb

Browse files
committed
Demonstrate alternative js_filename_to_index functions
1 parent 931fec7 commit c6d1deb

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/ram_bundle.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//! RAM bundle operations
2+
use regex::Regex;
23
use scroll::Pread;
34
use std::borrow::Cow;
45
use std::collections::BTreeMap;
@@ -191,6 +192,40 @@ impl<'a> RamBundle<'a> {
191192
}
192193
}
193194

195+
fn js_filename_to_index_original(filename: &str) -> Option<Result<usize>> {
196+
let module_regex = Regex::new(r"^(\d+)\.js$").unwrap();
197+
match module_regex.captures(filename) {
198+
Some(captures) => {
199+
let module_string = captures.get(1).unwrap().as_str();
200+
Some(
201+
module_string
202+
.parse::<usize>()
203+
.or(Err(Error::InvalidRamBundleIndex)),
204+
)
205+
}
206+
None => None,
207+
}
208+
}
209+
210+
/// Filename must be made of unicode digits and the .js extension
211+
/// Err if the filename uses non-ascii unicode numeric characters
212+
fn js_filename_to_index_compat(filename: &str) -> Option<Result<usize>> {
213+
match filename.rsplit_once(".js") {
214+
Some((basename, "")) => {
215+
if basename.chars().all(|x| x.is_numeric()) {
216+
Some(
217+
basename
218+
.parse::<usize>()
219+
.or(Err(Error::InvalidRamBundleIndex)),
220+
)
221+
} else {
222+
None
223+
}
224+
}
225+
_ => None,
226+
}
227+
}
228+
194229
/// Filename must be made of ascii-only digits and the .js extension
195230
/// Anything else errors with `Error::InvalidRamBundleIndex`
196231
fn js_filename_to_index_strict(filename: &str) -> Result<usize> {
@@ -201,6 +236,39 @@ fn js_filename_to_index_strict(filename: &str) -> Result<usize> {
201236
_ => Err(Error::InvalidRamBundleIndex),
202237
}
203238
}
239+
240+
/// Filename must be made of ascii-only digits and the .js extension
241+
/// Anything else returns None
242+
fn js_filename_to_index_lax(filename: &str) -> Option<usize> {
243+
match filename.rsplit_once(".js") {
244+
Some((basename, "")) => basename.parse::<usize>().map(Some).unwrap_or(None),
245+
_ => None,
246+
}
247+
}
248+
249+
#[test]
250+
fn test_js_filename_no_extension() -> std::result::Result<(), Box<dyn std::error::Error>> {
251+
let mut s = "123.js";
252+
assert!(js_filename_to_index_original(s).unwrap().is_err());
253+
assert!(js_filename_to_index_compat(s).unwrap().is_err());
254+
assert!(js_filename_to_index_strict(s).is_err());
255+
assert_eq!(js_filename_to_index_lax(s), None);
256+
257+
s = "123.js";
258+
assert_eq!(js_filename_to_index_original(s).unwrap().unwrap(), 123);
259+
assert_eq!(js_filename_to_index_compat(s).unwrap().unwrap(), 123);
260+
assert_eq!(js_filename_to_index_strict(s).unwrap(), 123);
261+
assert_eq!(js_filename_to_index_lax(s).unwrap(), 123);
262+
263+
s = "nope";
264+
assert!(js_filename_to_index_original(s).is_none());
265+
assert!(js_filename_to_index_compat(s).is_none());
266+
assert!(js_filename_to_index_strict(s).is_err());
267+
assert!(js_filename_to_index_lax(s).is_none());
268+
269+
Ok(())
270+
}
271+
204272
/// Represents a file RAM bundle
205273
///
206274
/// This RAM bundle type is mostly used on Android.

0 commit comments

Comments
 (0)