Files
rxing/src/oned/rss/expanded/binary_util.rs
2022-12-14 17:52:11 -06:00

149 lines
4.3 KiB
Rust

/*
* Copyright (C) 2010 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* These authors would like to acknowledge the Spanish Ministry of Industry,
* Tourism and Trade, for the support in the project TSI020301-2008-2
* "PIRAmIDE: Personalizable Interactions with Resources on AmI-enabled
* Mobile Dynamic Environments", led by Treelogic
* ( http://www.treelogic.com/ ):
*
* http://www.piramidepse.com/
*/
/**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
*/
use lazy_static::lazy_static;
use regex::Regex;
use crate::{common::BitArray, Exceptions};
lazy_static! {
static ref ONE: Regex = Regex::new("1").unwrap();
static ref ZERO: Regex = Regex::new("0").unwrap();
static ref SPACE: Regex = Regex::new(" ").unwrap();
}
/*
* Constructs a BitArray from a String like the one returned from BitArray.toString()
*/
pub fn buildBitArrayFromString(data: &str) -> Result<BitArray, Exceptions> {
let dotsAndXs = ZERO
.replace_all(&ONE.replace_all(data, "X"), ".")
.to_string();
let mut binary = BitArray::with_size(SPACE.replace_all(&dotsAndXs, "").chars().count());
let mut counter = 0;
for i in 0..dotsAndXs.chars().count() {
// for (int i = 0; i < dotsAndXs.length(); ++i) {
if i % 9 == 0 {
// spaces
if dotsAndXs.chars().nth(i).unwrap() != ' ' {
return Err(Exceptions::IllegalStateException(
"space expected".to_owned(),
));
}
continue;
}
let currentChar = dotsAndXs.chars().nth(i).unwrap();
if currentChar == 'X' || currentChar == 'x' {
binary.set(counter);
}
counter += 1;
}
Ok(binary)
}
pub fn buildBitArrayFromStringWithoutSpaces(data: &str) -> Result<BitArray, Exceptions> {
let mut sb = String::new();
// let dotsAndXs = ZERO.matcher(ONE.matcher(data).replaceAll("X")).replaceAll(".");
let dotsAndXs = ZERO
.replace_all(&ONE.replace_all(data, "X"), ".")
.to_string();
let mut current = 0;
while current < dotsAndXs.chars().count() {
sb.push(' ');
let mut i = 0;
while i < 8 && current < dotsAndXs.chars().count() {
// for (int i = 0; i < 8 && current < dotsAndXs.length(); ++i) {
sb.push(dotsAndXs.chars().nth(current).unwrap());
current += 1;
i += 1;
}
}
buildBitArrayFromString(&sb)
}
/**
* @author Pablo Orduña, University of Deusto (pablo.orduna@deusto.es)
*/
#[cfg(test)]
mod BinaryUtilTest {
#[test]
fn testBuildBitArrayFromString() {
let data = " ..X..X.. ..XXX... XXXXXXXX ........";
check(data);
let data = " XXX..X..";
check(data);
let data = " XX";
check(data);
let data = " ....XX.. ..XX";
check(data);
let data = " ....XX.. ..XX..XX ....X.X. ........";
check(data);
}
fn check(data: &str) {
let binary = super::buildBitArrayFromString(data).expect("check");
assert_eq!(data, binary.to_string());
}
#[test]
fn testBuildBitArrayFromStringWithoutSpaces() {
let data = " ..X..X.. ..XXX... XXXXXXXX ........";
checkWithoutSpaces(data);
let data = " XXX..X..";
checkWithoutSpaces(data);
let data = " XX";
checkWithoutSpaces(data);
let data = " ....XX.. ..XX";
checkWithoutSpaces(data);
let data = " ....XX.. ..XX..XX ....X.X. ........";
checkWithoutSpaces(data);
}
fn checkWithoutSpaces(data: &str) {
let dataWithoutSpaces = super::SPACE.replace_all(data, "");
let binary =
super::buildBitArrayFromStringWithoutSpaces(&dataWithoutSpaces).expect("success");
assert_eq!(data, binary.to_string());
}
}