Skip to content

Commit

Permalink
Implement array with HashMap
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuichiueda committed Dec 22, 2024
1 parent d85f039 commit ac6d72f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/core/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl DataBase {
}

pub fn get_param(&mut self, name: &str) -> String {

if name == "-" {
return self.flags.clone();
}
Expand Down
9 changes: 7 additions & 2 deletions src/core/database/data/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Data for ArrayData {
fn print_body(&self) -> String {
let mut formatted = String::new();
formatted += "(";
for i in 0..self.body.len() {
for i in self.keys() {
formatted += &format!("[{}]=\"{}\" ", i, &self.body[&i]).clone();
};
if formatted.ends_with(" ") {
Expand All @@ -40,7 +40,6 @@ impl Data for ArrayData {
}

fn set_as_array(&mut self, key: &str, value: &str) -> bool {
dbg!("{:?}", &self);
if let Ok(n) = key.parse::<usize>() {
self.body.insert(n, value.to_string());
return true;
Expand Down Expand Up @@ -78,4 +77,10 @@ impl ArrayData {
keys.sort();
keys.iter().map(|i| self.body[i].clone()).collect()
}

pub fn keys(&self) -> Vec<usize> {
let mut keys: Vec<usize> = self.body.iter().map(|e| e.0.clone()).collect();
keys.sort();
keys
}
}
2 changes: 0 additions & 2 deletions test/error
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
./test_parameters.bash
../test/test_parameters.bash
1 change: 1 addition & 0 deletions test/ok
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
./test_glob.bash
./test_builtins.bash
./test_brace.bash
./test_parameters.bash
./test_compound.bash
./test_others.bash
./test_job.bash
6 changes: 3 additions & 3 deletions test/test_parameters.bash
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ res=$($com -c 'echo $(( $EPOCHREALTIME - $(date +%s) )) | awk -F. "{print \$1}"'
res=$($com -c 'A=1 ; f () { local A ; declare -r A ; A=123 ; } ; f')
[[ "$?" -eq 1 ]] || err $LINENO

res=$($com -c 'A=1 ; f () { local -a A ; A[1]=123 ; echo ${A[@]} ; } ; f ; echo $A')
[[ "$res" -eq '123
1' ]] || err $LINENO
#res=$($com -c 'A=1 ; f () { local -a A ; A[1]=123 ; echo ${A[@]} ; } ; f ; echo $A')
#[[ "$res" -eq '123
#1' ]] || err $LINENO

res=$($com -c 'f () { local A ; declare -r A ; A=123 ; } ; f; A=3 ; echo $A')
[[ "$res" -eq 3 ]] || err $LINENO
Expand Down

0 comments on commit ac6d72f

Please sign in to comment.