gtool5 Fortran 90/95 ライブラリ 1.0.0-rc5
English
Loading...
Searching...
No Matches
dcstringcprintf.f90
Go to the documentation of this file.
1! -*- mode: f90; coding: utf-8 -*-
2!-----------------------------------------------------------------------
3! Copyright (c) 2000-2026 Gtool Development Group. All rights reserved.
4!-----------------------------------------------------------------------
5!>
6!> @author Yasuhiro MORIKAWA, Eizi TOYODA
7!> @copyright Copyright (C) GFD Dennou Club, 2000-2026. All rights reserved. <br/>
8!> License is BSD-2-Clause. see [COPYRIGHT](@ref COPYRIGHT) in detail
9!> @en
10!> @brief Formatted output conversion (function version)
11!> @details
12!> Format a string like C sprintf(3) and return it.
13!> Note that the implementation is quite different from C sprintf(3).
14!> @enden
15!>
16!> @ja
17!> @brief 書式変換出力 (関数版)
18!> @details
19!> C の sprintf(3) のように文字列をフォーマットして返します。
20!> ただし、実装は C の sprintf(3) とは大分違うのでご注意ください。
21!> @endja
22!>
23
24!> @en
25!> @brief Format string and return result
26!> @details
27!> Return a formatted string according to format string `fmt`.
28!> Use specifiers starting with '%' in `fmt`.
29!> Use '%%' to output a literal '%'.
30!> See dcstringsprintf.f90 for specifier details and examples.
31!>
32!> @param[in] fmt Format string with specifiers
33!> @param[in] i Integer array data for %d, %o, %x
34!> @param[in] r Single precision real array data for %r
35!> @param[in] d Double precision real array data for %f
36!> @param[in] L Logical array data for %b, %y
37!> @param[in] n Repeat counts for %* specifier
38!> @param[in] c1 First character string for %c
39!> @param[in] c2 Second character string for %c
40!> @param[in] c3 Third character string for %c
41!> @param[in] ca Character array for %a
42!> @return Formatted string
43!> @enden
44!>
45!> @ja
46!> @brief 書式変換した文字列を返す
47!> @details
48!> フォーマット文字列 `fmt` に従って変換された文字列を返します。
49!> `fmt` には '%' で始まる指示子を含む文字列を与えます。
50!> '%' を出力したい場合は '%%' と記述します。
51!> 指示子に関しての詳細や用例に関しては dcstringsprintf.f90 を参照ください。
52!>
53!> @param[in] fmt 指示子を含むフォーマット文字列
54!> @param[in] i %d, %o, %x 用の整数配列データ
55!> @param[in] r %r 用の単精度実数配列データ
56!> @param[in] d %f 用の倍精度実数配列データ
57!> @param[in] L %b, %y 用の論理配列データ
58!> @param[in] n %* 用の繰り返し回数
59!> @param[in] c1 %c 用の1番目の文字列
60!> @param[in] c2 %c 用の2番目の文字列
61!> @param[in] c3 %c 用の3番目の文字列
62!> @param[in] ca %a 用の文字配列
63!> @return 書式変換された文字列
64!> @endja
65function dcstringcprintf(fmt, i, r, d, L, n, c1, c2, c3, ca, maxlen) result(result)
66
67 use dc_types, only: string, dp, sp
68 use dc_string, only: printf
69 implicit none
70 character(len = STRING):: result
71 character(*), intent(in):: fmt
72 integer, intent(in), optional:: i(:), n(:)
73 real(sp), intent(in), optional:: r(:)
74 real(dp), intent(in), optional:: d(:)
75 logical, intent(in), optional:: l(:)
76 character(*), intent(in), optional:: c1, c2, c3
77 character(*), intent(in), optional:: ca(:)
78 integer, intent(in), optional:: maxlen
79 integer:: out_len
80 continue
81 call printf(result, fmt, i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca)
82 if (present(maxlen)) then
83 out_len = max(0, min(len(result), maxlen))
84 if (out_len == 0) then
85 result = ''
86 else if (out_len < len(result)) then
87 result = result(1:out_len)
88 end if
89 end if
90end function dcstringcprintf
91
92function dcstringcprintft(fmt, i, r, d, L, n, c1, c2, c3, ca, maxlen) result(result)
93
94 use dc_types, only: string, token, dp, sp
95 use dc_string, only: cprintf
96 implicit none
97 character(len = TOKEN):: result
98 character(*), intent(in):: fmt
99 integer, intent(in), optional:: i(:), n(:)
100 real(sp), intent(in), optional:: r(:)
101 real(dp), intent(in), optional:: d(:)
102 logical, intent(in), optional:: l(:)
103 character(*), intent(in), optional:: c1, c2, c3
104 character(*), intent(in), optional:: ca(:)
105 integer, intent(in), optional:: maxlen
106 character(len = STRING):: tmp
107 integer:: out_len
108 continue
109 if (present(maxlen)) then
110 tmp = cprintf(fmt, i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca, maxlen=maxlen)
111 out_len = max(0, min(len(result), maxlen))
112 else
113 tmp = cprintf(fmt, i=i, r=r, d=d, l=l, n=n, c1=c1, c2=c2, c3=c3, ca=ca)
114 out_len = len(result)
115 end if
116 result = ''
117 if (out_len > 0) then
118 result(1:out_len) = tmp(1:out_len)
119 end if
120end function dcstringcprintft
character(len=string) function dcstringcprintf(fmt, i, r, d, l, n, c1, c2, c3, ca, maxlen)
書式変換出力 (関数版)
character(len=token) function dcstringcprintft(fmt, i, r, d, l, n, c1, c2, c3, ca, maxlen)
文字型変数の操作
Definition dc_string.f90:83
種別型パラメタを提供します。
Definition dc_types.f90:55
integer, parameter, public token
単語やキーワードを保持する文字型変数の種別型パラメタ
Definition dc_types.f90:128
integer, parameter, public string
文字列を保持する 文字型変数の種別型パラメタ
Definition dc_types.f90:137
integer, parameter, public dp
倍精度実数型変数
Definition dc_types.f90:92
integer, parameter, public sp
単精度実数型変数
Definition dc_types.f90:82