|
1 | 1 | submodule(sourcery_string_m) sourcery_string_s |
| 2 | + use assert_m, only : assert |
| 3 | + use sourcery_m, only : csv |
2 | 4 | implicit none |
3 | 5 |
|
4 | 6 | contains |
|
7 | 9 | new_string%string_ = string |
8 | 10 | end procedure |
9 | 11 |
|
10 | | - module procedure string |
| 12 | + module procedure as_character |
11 | 13 | raw_string = self%string_ |
12 | 14 | end procedure |
13 | 15 |
|
|
37 | 39 |
|
38 | 40 | end procedure |
39 | 41 |
|
| 42 | + module procedure get_json_key |
| 43 | + character(len=:), allocatable :: raw_line |
| 44 | + |
| 45 | + raw_line = self%string() |
| 46 | + associate(opening_key_quotes => index(raw_line, '"'), separator => index(raw_line, ':')) |
| 47 | + associate(closing_key_quotes => opening_key_quotes + index(raw_line(opening_key_quotes+1:), '"')) |
| 48 | + unquoted_key = string_t(trim(raw_line(opening_key_quotes+1:closing_key_quotes-1))) |
| 49 | + end associate |
| 50 | + end associate |
| 51 | + |
| 52 | + end procedure |
| 53 | + |
| 54 | + module procedure get_json_real |
| 55 | + character(len=:), allocatable :: raw_line, string_value |
| 56 | + |
| 57 | + call assert(key==self%get_json_key(), "string_s(get_json_real): key==self%get_json_key()", key) |
| 58 | + |
| 59 | + raw_line = self%string() |
| 60 | + associate(text_after_colon => raw_line(index(raw_line, ':')+1:)) |
| 61 | + associate(trailing_comma => index(text_after_colon, ',')) |
| 62 | + if (trailing_comma == 0) then |
| 63 | + string_value = trim(adjustl((text_after_colon))) |
| 64 | + else |
| 65 | + string_value = trim(adjustl((text_after_colon(:trailing_comma-1)))) |
| 66 | + end if |
| 67 | + read(string_value, fmt=*) value_ |
| 68 | + end associate |
| 69 | + end associate |
| 70 | + |
| 71 | + end procedure |
| 72 | + |
| 73 | + module procedure get_json_string |
| 74 | + |
| 75 | + character(len=:), allocatable :: raw_line |
| 76 | + |
| 77 | + call assert(key==self%get_json_key(), "key==self%get_string_json()", key) |
| 78 | + |
| 79 | + raw_line = self%string() |
| 80 | + associate(text_after_colon => raw_line(index(raw_line, ':')+1:)) |
| 81 | + associate(opening_value_quotes => index(text_after_colon, '"')) |
| 82 | + associate(closing_value_quotes => opening_value_quotes + index(text_after_colon(opening_value_quotes+1:), '"')) |
| 83 | + if (any([opening_value_quotes, closing_value_quotes] == 0)) then |
| 84 | + value_ = string_t(trim(adjustl((text_after_colon)))) |
| 85 | + else |
| 86 | + value_ = string_t(text_after_colon(opening_value_quotes+1:closing_value_quotes-1)) |
| 87 | + end if |
| 88 | + end associate |
| 89 | + end associate |
| 90 | + end associate |
| 91 | + |
| 92 | + end procedure |
| 93 | + |
| 94 | + module procedure get_json_logical |
| 95 | + character(len=:), allocatable :: raw_line, string_value |
| 96 | + |
| 97 | + call assert(key==self%get_json_key(), "string_s(get_json_logical): key==self%get_json_key()", key) |
| 98 | + |
| 99 | + raw_line = self%string() |
| 100 | + associate(text_after_colon => raw_line(index(raw_line, ':')+1:)) |
| 101 | + associate(trailing_comma => index(text_after_colon, ',')) |
| 102 | + if (trailing_comma == 0) then |
| 103 | + string_value = trim(adjustl((text_after_colon))) |
| 104 | + else |
| 105 | + string_value = trim(adjustl((text_after_colon(:trailing_comma-1)))) |
| 106 | + end if |
| 107 | + call assert(string_value=="true" .or. string_value=="false", & |
| 108 | + 'string_s(get_json_logical): string_value=="true" .or. string_value="false"', string_value) |
| 109 | + value_ = string_value == "true" |
| 110 | + end associate |
| 111 | + end associate |
| 112 | + |
| 113 | + end procedure |
| 114 | + |
| 115 | + module procedure get_json_integer |
| 116 | + character(len=:), allocatable :: raw_line, string_value |
| 117 | + |
| 118 | + call assert(key==self%get_json_key(), "string_s(get_json_logical): key==self%get_json_key()", key) |
| 119 | + |
| 120 | + raw_line = self%string() |
| 121 | + associate(text_after_colon => raw_line(index(raw_line, ':')+1:)) |
| 122 | + associate(trailing_comma => index(text_after_colon, ',')) |
| 123 | + if (trailing_comma == 0) then |
| 124 | + string_value = trim(adjustl((text_after_colon))) |
| 125 | + else |
| 126 | + string_value = trim(adjustl((text_after_colon(:trailing_comma-1)))) |
| 127 | + end if |
| 128 | + read(string_value, fmt=*) value_ |
| 129 | + end associate |
| 130 | + end associate |
| 131 | + |
| 132 | + end procedure |
| 133 | + |
| 134 | + module procedure get_json_integer_array |
| 135 | + character(len=:), allocatable :: raw_line |
| 136 | + real, allocatable :: real_array(:) |
| 137 | + integer i |
| 138 | + |
| 139 | + call assert(key==self%get_json_key(), "string_s(get_json_integer_array): key==self%get_json_key()", key) |
| 140 | + |
| 141 | + raw_line = self%string() |
| 142 | + associate(colon => index(raw_line, ":")) |
| 143 | + associate(opening_bracket => colon + index(raw_line(colon+1:), "[")) |
| 144 | + associate(closing_bracket => opening_bracket + index(raw_line(opening_bracket+1:), "]")) |
| 145 | + associate(commas => count("," == [(raw_line(i:i), i=opening_bracket+1,closing_bracket-1)])) |
| 146 | + associate(num_inputs => commas + 1) |
| 147 | + allocate(real_array(num_inputs)) |
| 148 | + read(raw_line(opening_bracket+1:closing_bracket-1), fmt=*) real_array |
| 149 | + value_ = int(real_array) |
| 150 | + end associate |
| 151 | + end associate |
| 152 | + end associate |
| 153 | + end associate |
| 154 | + end associate |
| 155 | + |
| 156 | + end procedure |
| 157 | + |
| 158 | + module procedure equivalent |
| 159 | + lhs_eqv_rhs = lhs%string() == rhs%string() |
| 160 | + end procedure |
| 161 | + |
40 | 162 | end submodule sourcery_string_s |
0 commit comments