Skip to content

Add no_std support #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ documentation = "https://docs.rs/unicode-bidi/"
keywords = ["rtl", "unicode", "text", "layout", "bidi"]
readme="README.md"
edition = "2018"
categories = [
"no-std",
"encoding",
"text-processing",
]

# No data is shipped; benches, examples and tests also depend on data.
exclude = [
Expand All @@ -29,14 +34,16 @@ name = "unicode_bidi"
[dependencies]
flame = { version = "0.2", optional = true }
flamer = { version = "0.4", optional = true }
matches = "0.1"
serde = { version = ">=0.8, <2.0", optional = true, features = ["derive"] }
serde = { version = ">=0.8, <2.0", default-features = false, optional = true, features = ["derive"] }

[dev-dependencies]
serde_test = ">=0.8, <2.0"

[features]
default = []
# Note: We don't actually use the `std` feature for anything other than making
# doctests work. But it may come in handy in the future.
default = ["std"]
std = []
unstable = [] # travis-cargo needs it
bench_it = []
flame_it = ["flame", "flamer"]
Expand Down
4 changes: 2 additions & 2 deletions src/char_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ mod tables;

pub use self::tables::{BidiClass, UNICODE_VERSION};

use std::cmp::Ordering::{Equal, Less, Greater};
use std::char;
use core::cmp::Ordering::{Equal, Less, Greater};
use core::char;

use self::tables::bidi_class_table;
use crate::BidiClass::*;
Expand Down
2 changes: 2 additions & 0 deletions src/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

//! This module holds deprecated assets only.

use alloc::vec::Vec;

use super::*;

/// Find the level runs within a line and return them in visual order.
Expand Down
7 changes: 5 additions & 2 deletions src/explicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//!
//! <http://www.unicode.org/reports/tr9/#Explicit_Levels_and_Directions>

use matches::matches;
use alloc::vec::Vec;

use super::char_data::{BidiClass::{self, *}, is_rtl};
use super::level::Level;
Expand Down Expand Up @@ -46,7 +46,10 @@ pub fn compute(
let last_level = stack.last().level;

// X5a-X5c: Isolate initiators get the level of the last entry on the stack.
let is_isolate = matches!(original_classes[i], RLI | LRI | FSI);
let is_isolate = match original_classes[i] {
RLI | LRI | FSI => true,
_ => false,
};
if is_isolate {
levels[i] = last_level;
match stack.last().status {
Expand Down
9 changes: 6 additions & 3 deletions src/implicit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

//! 3.3.4 - 3.3.6. Resolve implicit levels and types.

use std::cmp::max;
use matches::matches;
use core::cmp::max;
use alloc::vec::Vec;

use super::char_data::BidiClass::{self, *};
use super::prepare::{IsolatingRunSequence, LevelRun, not_removed_by_x9, removed_by_x9};
Expand Down Expand Up @@ -223,5 +223,8 @@ pub fn resolve_levels(original_classes: &[BidiClass], levels: &mut [Level]) -> L
/// <http://www.unicode.org/reports/tr9/#NI>
#[allow(non_snake_case)]
fn is_NI(class: BidiClass) -> bool {
matches!(class, B | S | WS | ON | FSI | LRI | RLI | PDI)
match class {
B | S | WS | ON | FSI | LRI | RLI | PDI => true,
_ => false,
}
}
4 changes: 3 additions & 1 deletion src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
//!
//! <http://www.unicode.org/reports/tr9/#BD2>

use std::convert::{From, Into};
use alloc::vec::Vec;
use core::convert::{From, Into};
use alloc::string::{String, ToString};

use super::char_data::BidiClass;

Expand Down
24 changes: 20 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,24 @@
//! ]);
//! ```
//!
//! # Features
//!
//! - `std`: Enabled by default, but can be disabled to make `unicode_bidi`
//! `#![no_std]` + `alloc` compatible.
//! - `serde`: Adds [`serde::Serialize`] and [`serde::Deserialize`]
//! implementations to relevant types.
//!
//! [tr9]: <http://www.unicode.org/reports/tr9/>

#![forbid(unsafe_code)]

#![no_std]
// We need to link to std to make doc tests work on older Rust versions
#![cfg(feature = "std")]
extern crate std;
#[macro_use]
extern crate alloc;

pub mod deprecated;
pub mod format_chars;
pub mod level;
Expand All @@ -70,10 +84,12 @@ pub use crate::char_data::{BidiClass, bidi_class, UNICODE_VERSION};
pub use crate::level::{Level, LTR_LEVEL, RTL_LEVEL};
pub use crate::prepare::LevelRun;

use std::borrow::Cow;
use std::cmp::{max, min};
use std::iter::repeat;
use std::ops::Range;
use alloc::borrow::Cow;
use alloc::vec::Vec;
use alloc::string::String;
use core::cmp::{max, min};
use core::iter::repeat;
use core::ops::Range;

use crate::BidiClass::*;
use crate::format_chars as chars;
Expand Down
15 changes: 9 additions & 6 deletions src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
//!
//! <http://www.unicode.org/reports/tr9/#Preparations_for_Implicit_Processing>

use std::cmp::max;
use std::ops::Range;
use matches::matches;
use core::cmp::max;
use core::ops::Range;
use alloc::vec::Vec;

use super::BidiClass::{self, *};
use super::level::Level;
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn isolating_run_sequences(

sequence.push(run);

if matches!(end_class, RLI | LRI | FSI) {
if let RLI | LRI | FSI = end_class {
// Resume this sequence after the isolate.
stack.push(sequence);
} else {
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn isolating_run_sequences(
};

// Get the level of the next non-removed char after the runs.
let succ_level = if matches!(original_classes[end_of_seq - 1], RLI | LRI | FSI) {
let succ_level = if let RLI | LRI | FSI = original_classes[end_of_seq - 1] {
para_level
} else {
match original_classes[end_of_seq..].iter().position(
Expand Down Expand Up @@ -163,7 +163,10 @@ fn level_runs(levels: &[Level], original_classes: &[BidiClass]) -> Vec<LevelRun>
///
/// <http://www.unicode.org/reports/tr9/#X9>
pub fn removed_by_x9(class: BidiClass) -> bool {
matches!(class, RLE | LRE | RLO | LRO | PDF | BN)
match class {
RLE | LRE | RLO | LRO | PDF | BN => true,
_ => false,
}
}

// For use as a predicate for `position` / `rposition`
Expand Down