Skip to content

Commit

Permalink
feat(string_t):extract double-precision JSON value
Browse files Browse the repository at this point in the history
  • Loading branch information
rouson committed Sep 1, 2024
1 parent 92b9d9b commit ed57123
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/julienne/julienne_string_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,22 @@ module julienne_string_m
generic :: operator(/=) => string_t_ne_string_t, string_t_ne_character, character_ne_string_t
generic :: operator(==) => string_t_eq_string_t, string_t_eq_character, character_eq_string_t
generic :: assignment(= ) => assign_string_t_to_character, assign_character_to_string_t
generic :: get_json_value => get_real, get_real_with_character_key &
,get_string &
generic :: get_json_value => get_string &
,get_real, get_real_with_character_key &
,get_character, get_character_with_character_key &
,get_logical, get_logical_with_character_key &
,get_real_array ,get_real_array_with_character_key &
,get_integer_array, get_integer_array_with_character_key &
,get_integer, get_integer_with_character_key
,get_integer, get_integer_with_character_key &
,get_double_precision, get_double_precision_with_character_key
procedure, private :: get_real, get_real_with_character_key
procedure, private :: get_string
procedure, private :: get_logical, get_logical_with_character_key
procedure, private :: get_integer, get_integer_with_character_key
procedure, private :: get_real_array, get_real_array_with_character_key
procedure, private :: get_integer_array, get_integer_array_with_character_key
procedure, private :: get_character, get_character_with_character_key
procedure :: get_double_precision, get_double_precision_with_character_key
procedure, private :: string_t_ne_string_t, string_t_ne_character
procedure, private :: string_t_eq_string_t, string_t_eq_character
procedure, private :: assign_character_to_string_t
Expand Down Expand Up @@ -131,6 +133,21 @@ pure module function get_real_with_character_key(self, key, mold) result(value_)
real value_
end function

pure module function get_double_precision(self, key, mold) result(value_)
implicit none
class(string_t), intent(in) :: self, key
double precision, intent(in) :: mold
double precision value_
end function

pure module function get_double_precision_with_character_key(self, key, mold) result(value_)
implicit none
class(string_t), intent(in) :: self
character(len=*), intent(in) :: key
double precision, intent(in) :: mold
double precision value_
end function

pure module function get_character(self, key, mold) result(value_)
implicit none
class(string_t), intent(in) :: self, key
Expand Down
23 changes: 23 additions & 0 deletions src/julienne/julienne_string_s.f90
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,34 @@
value_ = self%get_real(string_t(key), mold)
end procedure

module procedure get_double_precision_with_character_key
value_ = self%get_double_precision(string_t(key), mold)
end procedure

module procedure get_real
character(len=:), allocatable :: raw_line, string_value

call assert(key==self%get_json_key(), "string_s(get_real): key==self%get_json_key()", key)

raw_line = self%string()
associate(text_after_colon => raw_line(index(raw_line, ':')+1:))
associate(trailing_comma => index(text_after_colon, ','))
if (trailing_comma == 0) then
string_value = trim(adjustl((text_after_colon)))
else
string_value = trim(adjustl((text_after_colon(:trailing_comma-1))))
end if
read(string_value, fmt=*) value_
end associate
end associate

end procedure

module procedure get_double_precision
character(len=:), allocatable :: raw_line, string_value

call assert(key==self%get_json_key(), "string_s(get_double_precision): key==self%get_json_key()", key)

raw_line = self%string()
associate(text_after_colon => raw_line(index(raw_line, ':')+1:))
associate(trailing_comma => index(text_after_colon, ','))
Expand Down
24 changes: 23 additions & 1 deletion test/string_test.F90
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function results() result(test_results)
(string_t("extracting a key string from a colon-separated key/value pair"), extracts_key), &
test_description_t &
(string_t("extracting a real value from a colon-separated key/value pair"), extracts_real_value), &
test_description_t &
(string_t("extracting a double-precision value from a colon-separated key/value pair"), extracts_double_precision_value), &
test_description_t &
(string_t("extracting a string value from a colon-separated key/value pair"), extracts_string_value), &
test_description_t &
Expand All @@ -74,7 +76,8 @@ function results() result(test_results)
check_allocation_ptr, supports_equivalence_ptr, supports_non_equivalence_ptr, supports_concatenation_ptr, &
assigns_string_ptr, assigns_character_ptr, constructs_from_integer_ptr, constructs_from_real_ptr, concatenates_ptr, &
extracts_key_ptr, extracts_real_ptr, extracts_string_ptr, extracts_logical_ptr, extracts_integer_array_ptr, &
extracts_real_array_ptr, extracts_integer_ptr, extracts_file_base_ptr, extracts_file_name_ptr, extracts_character_ptr
extracts_real_array_ptr, extracts_integer_ptr, extracts_file_base_ptr, extracts_file_name_ptr, extracts_character_ptr, &
extracts_double_precision_value_ptr

check_allocation_ptr => check_allocation
supports_equivalence_ptr => supports_equivalence_operator
Expand All @@ -87,6 +90,7 @@ function results() result(test_results)
concatenates_ptr => concatenates_elements
extracts_key_ptr => extracts_key
extracts_real_ptr => extracts_real_value
extracts_double_precision_value_ptr => extracts_double_precision_value
extracts_string_ptr => extracts_string_value
extracts_character_ptr => extracts_character_value
extracts_logical_ptr => extracts_logical_value
Expand All @@ -111,6 +115,8 @@ function results() result(test_results)
test_description_t(string_t('supporting unary operator(.cat.) for array arguments'), concatenates_ptr), &
test_description_t(string_t("extracting a key string from a colon-separated key/value pair"), extracts_key_ptr), &
test_description_t(string_t("extracting a real value from a colon-separated key/value pair"), extracts_real_ptr), &
test_description_t( &
string_t("extracting a double-precision value from a colon-separated key/value pair"), extracts_double_precision_value_ptr),&
test_description_t(string_t("extracting a string value from a colon-separated key/value pair"), extracts_string_ptr), &
test_description_t(string_t("extracting a character value from a colon-separated key/value pair"), extracts_character_ptr), &
test_description_t(string_t("extracting a logical value from a colon-separated key/value pair"), extracts_logical_ptr), &
Expand Down Expand Up @@ -155,6 +161,22 @@ function extracts_key() result(passed)
#endif
end function

function extracts_double_precision_value() result(passed)
logical passed

#ifndef _CRAYFTN
associate(line => string_t('"pi" : 3.141592653589793D0'))
passed = line%get_json_value(key="pi", mold=0.D0) == 3.141592653589793D0
end associate
#else
block
type(string_t) line
line = string_t('"pi" : 3.141592653589793D0')
passed = line%get_json_value(key="pi", mold=0.D0) == 3.141592653589793D0
end block
#endif
end function

function extracts_real_value() result(passed)
logical passed

Expand Down

0 comments on commit ed57123

Please sign in to comment.