@@ -39,106 +39,6 @@ std::int32_t RTNAME(ArgumentCount)() {
39
39
40
40
pid_t RTNAME (GetPID)() { return getpid (); }
41
41
42
- // Returns the length of the \p string. Assumes \p string is valid.
43
- static std::int64_t StringLength (const char *string) {
44
- std::size_t length{std::strlen (string)};
45
- if constexpr (sizeof (std::size_t ) < sizeof (std::int64_t )) {
46
- return static_cast <std::int64_t >(length);
47
- } else {
48
- std::size_t max{std::numeric_limits<std::int64_t >::max ()};
49
- return length > max ? 0 // Just fail.
50
- : static_cast <std::int64_t >(length);
51
- }
52
- }
53
-
54
- static bool IsValidCharDescriptor (const Descriptor *value) {
55
- return value && value->IsAllocated () &&
56
- value->type () == TypeCode (TypeCategory::Character, 1 ) &&
57
- value->rank () == 0 ;
58
- }
59
-
60
- static bool IsValidIntDescriptor (const Descriptor *length) {
61
- auto typeCode{length->type ().GetCategoryAndKind ()};
62
- // Check that our descriptor is allocated and is a scalar integer with
63
- // kind != 1 (i.e. with a large enough decimal exponent range).
64
- return length->IsAllocated () && length->rank () == 0 &&
65
- length->type ().IsInteger () && typeCode && typeCode->second != 1 ;
66
- }
67
-
68
- static void FillWithSpaces (const Descriptor &value, std::size_t offset = 0 ) {
69
- if (offset < value.ElementBytes ()) {
70
- std::memset (
71
- value.OffsetElement (offset), ' ' , value.ElementBytes () - offset);
72
- }
73
- }
74
-
75
- static std::int32_t CopyToDescriptor (const Descriptor &value,
76
- const char *rawValue, std::int64_t rawValueLength, const Descriptor *errmsg,
77
- std::size_t offset = 0 ) {
78
-
79
- std::int64_t toCopy{std::min (rawValueLength,
80
- static_cast <std::int64_t >(value.ElementBytes () - offset))};
81
- if (toCopy < 0 ) {
82
- return ToErrmsg (errmsg, StatValueTooShort);
83
- }
84
-
85
- std::memcpy (value.OffsetElement (offset), rawValue, toCopy);
86
-
87
- if (rawValueLength > toCopy) {
88
- return ToErrmsg (errmsg, StatValueTooShort);
89
- }
90
-
91
- return StatOk;
92
- }
93
-
94
- static std::int32_t CheckAndCopyToDescriptor (const Descriptor *value,
95
- const char *rawValue, const Descriptor *errmsg, std::size_t &offset) {
96
- bool haveValue{IsValidCharDescriptor (value)};
97
-
98
- std::int64_t len{StringLength (rawValue)};
99
- if (len <= 0 ) {
100
- if (haveValue) {
101
- FillWithSpaces (*value);
102
- }
103
- return ToErrmsg (errmsg, StatMissingArgument);
104
- }
105
-
106
- std::int32_t stat{StatOk};
107
- if (haveValue) {
108
- stat = CopyToDescriptor (*value, rawValue, len, errmsg, offset);
109
- }
110
-
111
- offset += len;
112
- return stat;
113
- }
114
-
115
- static void StoreLengthToDescriptor (
116
- const Descriptor *length, std::int64_t value, Terminator &terminator) {
117
- auto typeCode{length->type ().GetCategoryAndKind ()};
118
- int kind{typeCode->second };
119
- Fortran::runtime::ApplyIntegerKind<Fortran::runtime::StoreIntegerAt, void >(
120
- kind, terminator, *length, /* atIndex = */ 0 , value);
121
- }
122
-
123
- template <int KIND> struct FitsInIntegerKind {
124
- bool operator ()([[maybe_unused]] std::int64_t value) {
125
- if constexpr (KIND >= 8 ) {
126
- return true ;
127
- } else {
128
- return value <= std::numeric_limits<Fortran::runtime::CppTypeFor<
129
- Fortran::common::TypeCategory::Integer, KIND>>::max ();
130
- }
131
- }
132
- };
133
-
134
- static bool FitsInDescriptor (
135
- const Descriptor *length, std::int64_t value, Terminator &terminator) {
136
- auto typeCode{length->type ().GetCategoryAndKind ()};
137
- int kind{typeCode->second };
138
- return Fortran::runtime::ApplyIntegerKind<FitsInIntegerKind, bool >(
139
- kind, terminator, value);
140
- }
141
-
142
42
std::int32_t RTNAME (GetCommandArgument)(std::int32_t n, const Descriptor *value,
143
43
const Descriptor *length, const Descriptor *errmsg, const char *sourceFile,
144
44
int line) {
@@ -152,7 +52,7 @@ std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
152
52
// Store 0 in case we error out later on.
153
53
if (length) {
154
54
RUNTIME_CHECK (terminator, IsValidIntDescriptor (length));
155
- StoreLengthToDescriptor (length, 0 , terminator);
55
+ StoreIntToDescriptor (length, 0 , terminator);
156
56
}
157
57
158
58
if (n < 0 || n >= executionEnvironment.argc ) {
@@ -166,7 +66,7 @@ std::int32_t RTNAME(GetCommandArgument)(std::int32_t n, const Descriptor *value,
166
66
}
167
67
168
68
if (length && FitsInDescriptor (length, argLen, terminator)) {
169
- StoreLengthToDescriptor (length, argLen, terminator);
69
+ StoreIntToDescriptor (length, argLen, terminator);
170
70
}
171
71
172
72
if (value) {
@@ -188,7 +88,7 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
188
88
// Store 0 in case we error out later on.
189
89
if (length) {
190
90
RUNTIME_CHECK (terminator, IsValidIntDescriptor (length));
191
- StoreLengthToDescriptor (length, 0 , terminator);
91
+ StoreIntToDescriptor (length, 0 , terminator);
192
92
}
193
93
194
94
auto shouldContinue = [&](std::int32_t stat) -> bool {
@@ -225,7 +125,7 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
225
125
}
226
126
227
127
if (length && FitsInDescriptor (length, offset, terminator)) {
228
- StoreLengthToDescriptor (length, offset, terminator);
128
+ StoreIntToDescriptor (length, offset, terminator);
229
129
}
230
130
231
131
// value += spaces for padding
@@ -236,14 +136,6 @@ std::int32_t RTNAME(GetCommand)(const Descriptor *value,
236
136
return stat;
237
137
}
238
138
239
- static std::size_t LengthWithoutTrailingSpaces (const Descriptor &d) {
240
- std::size_t s{d.ElementBytes () - 1 };
241
- while (*d.OffsetElement (s) == ' ' ) {
242
- --s;
243
- }
244
- return s + 1 ;
245
- }
246
-
247
139
std::int32_t RTNAME (GetEnvVariable)(const Descriptor &name,
248
140
const Descriptor *value, const Descriptor *length, bool trim_name,
249
141
const Descriptor *errmsg, const char *sourceFile, int line) {
@@ -257,7 +149,7 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
257
149
// Store 0 in case we error out later on.
258
150
if (length) {
259
151
RUNTIME_CHECK (terminator, IsValidIntDescriptor (length));
260
- StoreLengthToDescriptor (length, 0 , terminator);
152
+ StoreIntToDescriptor (length, 0 , terminator);
261
153
}
262
154
263
155
const char *rawValue{nullptr };
@@ -273,7 +165,7 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
273
165
274
166
std::int64_t varLen{StringLength (rawValue)};
275
167
if (length && FitsInDescriptor (length, varLen, terminator)) {
276
- StoreLengthToDescriptor (length, varLen, terminator);
168
+ StoreIntToDescriptor (length, varLen, terminator);
277
169
}
278
170
279
171
if (value) {
0 commit comments