mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-22 16:33:29 +00:00
allow field widths (like '%1d') in formats
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/branches/testplant_1@29027 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
a3687802a4
commit
fcb54b32cd
1 changed files with 120 additions and 86 deletions
|
@ -843,6 +843,8 @@ static inline int getDigits(const char *from, char *to, int limit, BOOL *error)
|
|||
{
|
||||
// Skip '%'
|
||||
formatIdx++;
|
||||
while (format[formatIdx] >= '0' && format[formatIdx] <= '9' && formatIdx < formatLen)
|
||||
formatIdx++; // skip field width
|
||||
|
||||
switch (format[formatIdx])
|
||||
{
|
||||
|
@ -1312,12 +1314,12 @@ static inline int getDigits(const char *from, char *to, int limit, BOOL *error)
|
|||
}
|
||||
|
||||
d = [[NSCalendarDate alloc] initWithYear: year
|
||||
month: 1
|
||||
day: 1
|
||||
hour: 0
|
||||
minute: 0
|
||||
second: 0
|
||||
timeZone: gmtZone];
|
||||
month: 1
|
||||
day: 1
|
||||
hour: 0
|
||||
minute: 0
|
||||
second: 0
|
||||
timeZone: gmtZone];
|
||||
currDay = [d dayOfWeek];
|
||||
RELEASE(d);
|
||||
|
||||
|
@ -1783,6 +1785,21 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
}
|
||||
}
|
||||
|
||||
#define MAX_FLD_WIDTH 99
|
||||
|
||||
static void outputValueWithFormat(int v, char *fldfmt, DescriptionInfo *info)
|
||||
{
|
||||
char cbuf[MAX_FLD_WIDTH + 1];
|
||||
int idx = 0;
|
||||
|
||||
sprintf((char*)cbuf, fldfmt, v);
|
||||
Grow(info, strlen((char*)cbuf));
|
||||
while (cbuf[idx] != '\0')
|
||||
{
|
||||
info->t[info->offset++] = cbuf[idx++];
|
||||
}
|
||||
}
|
||||
|
||||
- (void) _format: (NSString*)fmt
|
||||
locale: (NSDictionary*)locale
|
||||
info: (DescriptionInfo*)info
|
||||
|
@ -1814,11 +1831,27 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
BOOL mname = NO;
|
||||
BOOL dname = NO;
|
||||
BOOL twelve = NO;
|
||||
char fldfmt[8];
|
||||
int fmtlen = 0;
|
||||
int width = 0;
|
||||
|
||||
// Only care about a format specifier
|
||||
if (f[i] == '%')
|
||||
{
|
||||
i++;
|
||||
fldfmt[fmtlen++] = '%'; // start format with %
|
||||
while (fmtlen < 5 && f[i] >= '0' && f[i] <= '9') // field width specified
|
||||
{
|
||||
fldfmt[fmtlen++] = f[i];
|
||||
width = 10 * width + f[i] - '0';
|
||||
i++;
|
||||
}
|
||||
if (fmtlen >= 5 || width > MAX_FLD_WIDTH)
|
||||
{
|
||||
// ignore formats that specify field width greater than the max allowed
|
||||
i -= fmtlen; // set i back so all ignored characters will be copied
|
||||
}
|
||||
|
||||
// check the character that comes after
|
||||
switch (f[i++])
|
||||
{
|
||||
|
@ -1875,41 +1908,25 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
v = info->yd;
|
||||
if (ycent)
|
||||
{
|
||||
if (v >= 0 && v <= 9999)
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
Grow(info, 4);
|
||||
info->t[info->offset+3] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+2] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 4;
|
||||
}
|
||||
else
|
||||
{
|
||||
unsigned char tmp[16];
|
||||
int idx = 0;
|
||||
|
||||
sprintf((char*)tmp, "%d", v);
|
||||
Grow(info, strlen((char*)tmp));
|
||||
while (tmp[idx] != '\0')
|
||||
{
|
||||
info->t[info->offset++] = tmp[idx++];
|
||||
}
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '4';
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Grow(info, 2);
|
||||
if (v < 0) v = -v;
|
||||
v = v % 100;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
// is it the month
|
||||
|
@ -1944,40 +1961,41 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
if (mtag == NO)
|
||||
{
|
||||
v = info->md;
|
||||
Grow(info, 2);
|
||||
v = v % 100;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'd': // day of month with leading zero
|
||||
v = info->dom;
|
||||
Grow(info, 2);
|
||||
v = v % 100;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
case 'e': // day of month with leading space
|
||||
v = info->dom;
|
||||
Grow(info, 2);
|
||||
v = v % 100;
|
||||
if (v%10 == '0')
|
||||
{
|
||||
info->t[info->offset+1] = ' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
}
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '1'; // no leading space, just like Cocoa
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
case 'F': // milliseconds
|
||||
|
@ -1991,24 +2009,26 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
s *= 1000.0;
|
||||
v = (NSInteger)(s + 0.5);
|
||||
}
|
||||
Grow(info, 3);
|
||||
info->t[info->offset+2] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 3;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '3';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
case 'j': // day of year
|
||||
v = [self dayOfYear];
|
||||
Grow(info, 3);
|
||||
info->t[info->offset+2] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 3;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '3';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
// is it the week-day
|
||||
|
@ -2044,8 +2064,13 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
}
|
||||
if (dtag == NO)
|
||||
{
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 1;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '1';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
@ -2066,31 +2091,40 @@ static void Grow(DescriptionInfo *info, unsigned size)
|
|||
v = v % 12;
|
||||
}
|
||||
}
|
||||
Grow(info, 2);
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
// is it the minute
|
||||
case 'M':
|
||||
v = info->mnd;
|
||||
Grow(info, 2);
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
// is it the second
|
||||
case 'S':
|
||||
v = info->sd;
|
||||
Grow(info, 2);
|
||||
info->t[info->offset+1] = (v%10) + '0';
|
||||
v /= 10;
|
||||
info->t[info->offset+0] = (v%10) + '0';
|
||||
info->offset += 2;
|
||||
if (fmtlen == 1) // no format width specified; supply default
|
||||
{
|
||||
fldfmt[fmtlen++] = '0';
|
||||
fldfmt[fmtlen++] = '2';
|
||||
}
|
||||
fldfmt[fmtlen++] = 'd';
|
||||
fldfmt[fmtlen++] = 0;
|
||||
outputValueWithFormat(v, fldfmt, info);
|
||||
break;
|
||||
|
||||
// Is it the am/pm indicator
|
||||
|
|
Loading…
Reference in a new issue