From 731428f798b8033646e62732359098ac0de0cf08 Mon Sep 17 00:00:00 2001 From: D0A1V2I3D Date: Sun, 5 Feb 2023 11:35:47 +0100 Subject: [PATCH] fix vec trail --- Cargo.toml | 12 ++++++++++ LICENSE | 21 +++++++++++++++++ README.md | 3 +++ src/flag_utils.rs | 16 +++++++++++++ src/lib.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++++ src/macros.rs | 15 ++++++++++++ 6 files changed, 126 insertions(+) create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 src/flag_utils.rs create mode 100644 src/lib.rs create mode 100644 src/macros.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..167d7b1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "binf" +version = "0.2.1" +edition = "2021" +license = "MIT" +description = "A crate that adds utilities for dealing with binary flags" +authors = ["0David "] +readme = "README.md" +repository = "https://github.com/D0A1V2I3D/binf" +documentation = "https://docs.rs/binf/" + +[dependencies] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ee4efa3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2022 0David + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3f95e2 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +documentation and better readme coming soon + +any contribution (bug report, issue, feature request, pull request) is welcome \ No newline at end of file diff --git a/src/flag_utils.rs b/src/flag_utils.rs new file mode 100644 index 0000000..fd5f038 --- /dev/null +++ b/src/flag_utils.rs @@ -0,0 +1,16 @@ +pub fn get_flags(flags: i32) -> Vec { + let mut fvec: Vec = Vec::new(); + let mut rest = flags; + while rest != 0 { + fvec.push((rest % 2) != 0); + rest = rest / 2; + } + + let mut tvec: Vec = Vec::new(); + for (i, value) in fvec.iter().enumerate() { + if *value { + tvec.push(i as i32); + } + } + tvec +} \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c548451 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,59 @@ +/*! +TODO documentation +*/ + +pub mod flag_utils; +pub mod macros; + +///struct that holds data for a flag and useful functions +pub struct Flag { + value: u128, +} + +impl Flag { + /// set a flag value + pub fn set_flag(&mut self, flag: u32, value: bool) { + if value { + self.value |= 1 << flag; + } else { + if self.get_flag(flag) { + self.value -= 1 << flag; + } + } + } + + /// get a value of flag + pub fn get_flag(&self, flag: u32) -> bool { + (self.value & (1 << flag)) != 0 + } + + /// returns a vector with all enabled flags + pub fn get_all_flags(&self) -> Vec { + let mut ret = vec![]; + for i in 0..128 { + ret.push(&self.value & (1u128 << i) != 0); + } + for i in (0..ret.len()).rev() { + if ret[i] { + ret.truncate(i + 1); + break; + } + } + ret + } + + /// creates Flag from int + pub fn new_from_value(value: u128) -> Self { + Self {value} + } + + /// returns flags value + pub fn get(&self) -> u128 { + self.value + } + + /// initializes a Flag with 0, use flag_new! macro to create with flags instead + pub fn new() -> Self { + Self { value: 0, } + } +} diff --git a/src/macros.rs b/src/macros.rs new file mode 100644 index 0000000..c69912b --- /dev/null +++ b/src/macros.rs @@ -0,0 +1,15 @@ +//#[macro_use] +/// macro that creates new Flag +/// for example: flag_new![3, 7] makes a flag that has those flags enabled (136) +#[macro_export] +macro_rules! flag_new { + ( $($f:expr),* ) => { + { + let mut tflag = crate::Flag::new(); + $( + tflag.set_flag($f, true); + )* + tflag + } + } +}