This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
forked from trishume/syntect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request trishume#3 from trishume/lazy-regex-compile
Binary dumps and lazy regex compilation
- Loading branch information
Showing
13 changed files
with
245 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
target | ||
Cargo.lock | ||
# Ignore the dumps for now until the format stabilizes | ||
assets/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
extern crate syntect; | ||
use syntect::package_set::PackageSet; | ||
|
||
fn main() { | ||
let mut ps = PackageSet::new(); | ||
ps.load_syntaxes("testdata/Packages", true).unwrap(); | ||
ps.dump_to_file("assets/default_newlines.packdump").unwrap(); | ||
|
||
let mut ps2 = PackageSet::new(); | ||
ps2.load_syntaxes("testdata/Packages", false).unwrap(); | ||
ps2.dump_to_file("assets/default_nonewlines.packdump").unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use bincode::SizeLimit; | ||
use bincode::rustc_serialize::*; | ||
use std::fs::File; | ||
use std::io::BufReader; | ||
use package_set::PackageSet; | ||
use std::path::Path; | ||
|
||
impl PackageSet { | ||
pub fn dump_binary(&self) -> Vec<u8> { | ||
assert!(!self.is_linked); | ||
encode(self, SizeLimit::Infinite).unwrap() | ||
} | ||
|
||
pub fn dump_to_file<P: AsRef<Path>>(&self, path: P) -> EncodingResult<()> { | ||
let mut f = try!(File::create(path).map_err(EncodingError::IoError)); | ||
encode_into(self, &mut f, SizeLimit::Infinite) | ||
} | ||
|
||
/// Returns a fully loaded and linked package set from | ||
/// a binary dump. Panics if the dump is invalid. | ||
pub fn from_binary(v: Vec<u8>) -> PackageSet { | ||
let mut ps: PackageSet = decode(&v[..]).unwrap(); | ||
ps.link_syntaxes(); | ||
ps | ||
} | ||
|
||
/// Returns a fully loaded and linked package set from | ||
/// a binary dump file. | ||
pub fn from_dump_file<P: AsRef<Path>>(path: P) -> DecodingResult<PackageSet> { | ||
let f = try!(File::open(path).map_err(DecodingError::IoError)); | ||
let mut reader = BufReader::new(f); | ||
decode_from(&mut reader, SizeLimit::Infinite) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use package_set::PackageSet; | ||
#[test] | ||
fn can_dump_and_load() { | ||
let mut ps = PackageSet::new(); | ||
ps.load_syntaxes("testdata/Packages", false).unwrap(); | ||
|
||
let bin = ps.dump_binary(); | ||
let ps2 = PackageSet::from_binary(bin); | ||
assert_eq!(ps.syntaxes.len(), ps2.syntaxes.len()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.