1####################################################################################################################################
2# COMMON STRING MODULE
3####################################################################################################################################
4package pgBackRestDoc::Common::String;
5
6use strict;
7use warnings FATAL => qw(all);
8use Carp qw(confess longmess);
9
10use Exporter qw(import);
11    our @EXPORT = qw();
12use File::Basename qw(dirname);
13
14####################################################################################################################################
15# trim
16#
17# Trim whitespace.
18####################################################################################################################################
19sub trim
20{
21    my $strBuffer = shift;
22
23    if (!defined($strBuffer))
24    {
25        return;
26    }
27
28    $strBuffer =~ s/^\s+|\s+$//g;
29
30    return $strBuffer;
31}
32
33push @EXPORT, qw(trim);
34
35####################################################################################################################################
36# coalesce - return first defined parameter
37####################################################################################################################################
38sub coalesce
39{
40    foreach my $strParam (@_)
41    {
42        if (defined($strParam))
43        {
44            return $strParam;
45        }
46    }
47
48    return;
49}
50
51push @EXPORT, qw(coalesce);
52
53####################################################################################################################################
54# timestampFormat
55#
56# Get standard timestamp format (or formatted as specified).
57####################################################################################################################################
58sub timestampFormat
59{
60    my $strFormat = shift;
61    my $lTime = shift;
62
63    if (!defined($strFormat))
64    {
65        $strFormat = '%4d-%02d-%02d %02d:%02d:%02d';
66    }
67
68    if (!defined($lTime))
69    {
70        $lTime = time();
71    }
72
73    my ($iSecond, $iMinute, $iHour, $iMonthDay, $iMonth, $iYear, $iWeekDay, $iYearDay, $bIsDst) = localtime($lTime);
74
75    if ($strFormat eq "%4d")
76    {
77        return sprintf($strFormat, $iYear + 1900)
78    }
79    else
80    {
81        return sprintf($strFormat, $iYear + 1900, $iMonth + 1, $iMonthDay, $iHour, $iMinute, $iSecond);
82    }
83}
84
85push @EXPORT, qw(timestampFormat);
86
87####################################################################################################################################
88# stringSplit
89####################################################################################################################################
90sub stringSplit
91{
92    my $strString = shift;
93    my $strChar = shift;
94    my $iLength = shift;
95
96    if (length($strString) <= $iLength)
97    {
98        return $strString, undef;
99    }
100
101    my $iPos = index($strString, $strChar);
102
103    if ($iPos == -1)
104    {
105        return $strString, undef;
106    }
107
108    my $iNewPos = $iPos;
109
110    while ($iNewPos != -1 && $iNewPos + 1 < $iLength)
111    {
112        $iPos = $iNewPos;
113        $iNewPos = index($strString, $strChar, $iPos + 1);
114    }
115
116    return substr($strString, 0, $iPos + 1), substr($strString, $iPos + 1);
117}
118
119push @EXPORT, qw(stringSplit);
120
1211;
122