Skip to content

Commit 344cbdc

Browse files
author
Yi Wu
committed
use copyBufferAndPad instead of str.fill and std::memcpy
Take copyBufferAndPad out of anonymous namespace and declear in header file.
1 parent bc215a1 commit 344cbdc

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

flang/include/flang/Runtime/time-intrinsic.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
// Defines the API between compiled code and the implementations of time-related
1010
// intrinsic subroutines in the runtime library.
1111

12+
// time-intrinsic.h
13+
#ifndef TIME_INTRINSIC_H
14+
#define TIME_INTRINSIC_H
15+
16+
#include <cstddef>
17+
18+
void copyBufferAndPad(
19+
char *dest, std::size_t destChars, char *buffer, std::size_t len);
20+
21+
#endif // TIME_INTRINSIC_H
22+
1223
#ifndef FORTRAN_RUNTIME_TIME_INTRINSIC_H_
1324
#define FORTRAN_RUNTIME_TIME_INTRINSIC_H_
1425

flang/runtime/extensions.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "flang/Runtime/command.h"
1515
#include "flang/Runtime/descriptor.h"
1616
#include "flang/Runtime/io-api.h"
17+
#include "flang/Runtime/time-intrinsic.h" // copyBufferAndPad
1718
#include <ctime>
1819

1920
#ifdef _WIN32
@@ -47,24 +48,21 @@ void FORTRAN_PROCEDURE_NAME(flush)(const int &unit) {
4748
std::int32_t FORTRAN_PROCEDURE_NAME(iargc)() { return RTNAME(ArgumentCount)(); }
4849

4950
void FORTRAN_PROCEDURE_NAME(fdate)(std::int8_t *arg, std::int64_t length) {
50-
std::array<char, 26> str;
5151
// If the length is too short to fit completely, blank return.
5252
if (length < 24) {
53-
str.fill(' ');
54-
strncpy(reinterpret_cast<char *>(arg), str.data(), length);
53+
copyBufferAndPad(reinterpret_cast<char *>(arg), length, nullptr, 0);
5554
return;
5655
}
5756

57+
std::array<char, 26> str;
5858
Terminator terminator{__FILE__, __LINE__};
5959
std::time_t current_time;
6060
std::time(&current_time);
6161
// Day Mon dd hh:mm:ss yyyy\n\0 is 26 characters, e.g.
6262
// Tue May 26 21:51:03 2015\n\0
6363

6464
ctime_alloc(str.data(), str.size(), current_time, terminator);
65-
66-
std::fill(str.begin() + 24, str.begin() + length, ' ');
67-
strncpy(reinterpret_cast<char *>(arg), str.data(), length);
65+
copyBufferAndPad(reinterpret_cast<char *>(arg), length, str.data(), 24);
6866
}
6967

7068
// CALL GETARG(N, ARG)

flang/runtime/time-intrinsic.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@
3939
// overload will have a dummy parameter whose type indicates whether or not it
4040
// should be preferred. Any other parameters required for SFINAE should have
4141
// default values provided.
42+
43+
void copyBufferAndPad(
44+
char *dest, std::size_t destChars, char *buffer, std::size_t len) {
45+
auto copyLen{std::min(len, destChars)};
46+
std::memcpy(dest, buffer, copyLen);
47+
for (auto i{copyLen}; i < destChars; ++i) {
48+
dest[i] = ' ';
49+
}
50+
}
51+
4252
namespace {
4353
// Types for the dummy parameter indicating the priority of a given overload.
4454
// We will invoke our helper with an integer literal argument, so the overload
@@ -279,29 +289,21 @@ static void GetDateAndTime(Fortran::runtime::Terminator &terminator, char *date,
279289

280290
static constexpr std::size_t buffSize{16};
281291
char buffer[buffSize];
282-
auto copyBufferAndPad{
283-
[&](char *dest, std::size_t destChars, std::size_t len) {
284-
auto copyLen{std::min(len, destChars)};
285-
std::memcpy(dest, buffer, copyLen);
286-
for (auto i{copyLen}; i < destChars; ++i) {
287-
dest[i] = ' ';
288-
}
289-
}};
290292
if (date) {
291293
auto len = std::strftime(buffer, buffSize, "%Y%m%d", &localTime);
292-
copyBufferAndPad(date, dateChars, len);
294+
copyBufferAndPad(date, dateChars, buffer, len);
293295
}
294296
if (time) {
295297
auto len{std::snprintf(buffer, buffSize, "%02d%02d%02d.%03jd",
296298
localTime.tm_hour, localTime.tm_min, localTime.tm_sec, ms)};
297-
copyBufferAndPad(time, timeChars, len);
299+
copyBufferAndPad(time, timeChars, buffer, len);
298300
}
299301
if (zone) {
300302
// Note: this may leave the buffer empty on many platforms. Classic flang
301303
// has a much more complex way of doing this (see __io_timezone in classic
302304
// flang).
303305
auto len{std::strftime(buffer, buffSize, "%z", &localTime)};
304-
copyBufferAndPad(zone, zoneChars, len);
306+
copyBufferAndPad(zone, zoneChars, buffer, len);
305307
}
306308
if (values) {
307309
auto typeCode{values->type().GetCategoryAndKind()};

0 commit comments

Comments
 (0)