ruby-changes:23583
From: tadf <ko1@a...>
Date: Sun, 13 May 2012 18:14:56 +0900 (JST)
Subject: [ruby-changes:23583] tadf:r35634 (trunk): * ext/date/date_strftime.c: removed unused code and changed the style.
tadf 2012-05-13 18:14:46 +0900 (Sun, 13 May 2012) New Revision: 35634 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=35634 Log: * ext/date/date_strftime.c: removed unused code and changed the style. Modified files: trunk/ChangeLog trunk/ext/date/date_strftime.c Index: ChangeLog =================================================================== --- ChangeLog (revision 35633) +++ ChangeLog (revision 35634) @@ -1,3 +1,7 @@ +Sun May 13 18:10:43 2012 Tadayoshi Funaba <tadf@d...> + + * ext/date/date_strftime.c: removed unused code and changed the style. + Sun May 13 17:37:56 2012 Tadayoshi Funaba <tadf@d...> * ext/date/date_strftime.c: refactored. Index: ext/date/date_strftime.c =================================================================== --- ext/date/date_strftime.c (revision 35633) +++ ext/date/date_strftime.c (revision 35634) @@ -1,169 +1,30 @@ -/* -*- c-file-style: "linux" -*- */ - /* - * strftime.c - * - * Public-domain implementation of ANSI C library routine. - * - * It's written in old-style C for maximal portability. - * However, since I'm used to prototypes, I've included them too. - * - * If you want stuff in the System V ascftime routine, add the SYSV_EXT define. - * For extensions from SunOS, add SUNOS_EXT. - * For stuff needed to implement the P1003.2 date command, add POSIX2_DATE. - * For VMS dates, add VMS_EXT. - * For a an RFC822 time format, add MAILHEADER_EXT. - * For ISO week years, add ISO_DATE_EXT. - * For complete POSIX semantics, add POSIX_SEMANTICS. - * - * The code for %c, %x, and %X now follows the 1003.2 specification for - * the POSIX locale. - * This version ignores LOCALE information. - * It also doesn't worry about multi-byte characters. - * So there. - * - * This file is also shipped with GAWK (GNU Awk), gawk specific bits of - * code are included if GAWK is defined. - * - * Arnold Robbins - * January, February, March, 1991 - * Updated March, April 1992 - * Updated April, 1993 - * Updated February, 1994 - * Updated May, 1994 - * Updated January, 1995 - * Updated September, 1995 - * Updated January, 1996 - * - * Fixes from ado@e... - * February 1991, May 1992 - * Fixes from Tor Lillqvist tml@t... - * May, 1993 - * Further fixes from ado@e... - * February 1994 - * %z code from chip@c... - * Applied September 1995 - * %V code fixed (again) and %G, %g added, - * January 1996 + date_strftime.c: based on a public-domain implementation of ANSI C + library routine of strftime, which is originally written by Arnold + Robbins. */ #include "ruby/ruby.h" #include "date_tmx.h" -#ifndef GAWK #include <stdio.h> -#include <ctype.h> +#include <stdlib.h> #include <string.h> +#include <ctype.h> #include <time.h> -#include <sys/types.h> -#include <errno.h> -#endif -#if defined(TM_IN_SYS_TIME) || !defined(GAWK) -#include <sys/types.h> + #if HAVE_SYS_TIME_H #include <sys/time.h> #endif -#endif + +#include <sys/types.h> #include <math.h> +#include <errno.h> -/* defaults: season to taste */ -#define SYSV_EXT 1 /* stuff in System V ascftime routine */ -#define SUNOS_EXT 1 /* stuff in SunOS strftime routine */ -#define POSIX2_DATE 1 /* stuff in Posix 1003.2 date command */ -#define VMS_EXT 1 /* include %v for VMS date format */ -#define MAILHEADER_EXT 1 /* add %z for HHMM format */ -#define ISO_DATE_EXT 1 /* %G and %g for year of ISO week */ - -#if defined(ISO_DATE_EXT) -#if ! defined(POSIX2_DATE) -#define POSIX2_DATE 1 -#endif -#endif - -#if defined(POSIX2_DATE) -#if ! defined(SYSV_EXT) -#define SYSV_EXT 1 -#endif -#if ! defined(SUNOS_EXT) -#define SUNOS_EXT 1 -#endif -#endif - -#if defined(POSIX2_DATE) -#define adddecl(stuff) stuff -#else -#define adddecl(stuff) -#endif - #undef strchr /* avoid AIX weirdness */ -#if 0 -#if !defined __STDC__ && !defined _WIN32 -#define const /**/ -static int weeknumber(); -adddecl(static int iso8601wknum();) -static int weeknumber_v(); -adddecl(static int iso8601wknum_v();) -#else -static int weeknumber(const struct tm *timeptr, int firstweekday); -adddecl(static int iso8601wknum(const struct tm *timeptr);) -static int weeknumber_v(const struct tmx *tmx, int firstweekday); -adddecl(static int iso8601wknum_v(const struct tmx *tmx);) -#endif -#endif - -#ifdef STDC_HEADERS -#include <stdlib.h> -#include <string.h> -#else -extern void *malloc(); -extern void *realloc(); -extern char *getenv(); -extern char *strchr(); -#endif - -#if 0 -#define range(low, item, hi) max((low), min((item), (hi))) -#else #define range(low, item, hi) (item) -#endif -#undef min /* just in case */ - -/* min --- return minimum of two numbers */ - -#ifndef __STDC__ -static inline int -min(a, b) -int a, b; -#else -static inline int -min(int a, int b) -#endif -{ - return (a < b ? a : b); -} - -#undef max /* also, just in case */ - -/* max --- return maximum of two numbers */ - -#ifndef __STDC__ -static inline int -max(a, b) -int a, b; -#else -static inline int -max(int a, int b) -#endif -{ - return (a > b ? a : b); -} - -#ifdef NO_STRING_LITERAL_CONCATENATION -#error No string literal concatenation -#endif - #define add(x,y) (rb_funcall((x), '+', 1, (y))) #define sub(x,y) (rb_funcall((x), '-', 1, (y))) #define mul(x,y) (rb_funcall((x), '*', 1, (y))) @@ -177,965 +38,599 @@ date_strftime_with_tmx(char *s, size_t maxsize, const char *format, const struct tmx *tmx) { - char *endp = s + maxsize; - char *start = s; - const char *sp, *tp; - auto char tbuf[100]; - ptrdiff_t i; - int v, w; - int precision, flags, colons; - char padding; - enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E}; + char *endp = s + maxsize; + char *start = s; + const char *sp, *tp; + auto char tbuf[100]; + ptrdiff_t i; + int v, w; + int precision, flags, colons; + char padding; + enum {LEFT, CHCASE, LOWER, UPPER, LOCALE_O, LOCALE_E}; #define BIT_OF(n) (1U<<(n)) - /* various tables for locale C */ - static const char days_l[][10] = { - "Sunday", "Monday", "Tuesday", "Wednesday", - "Thursday", "Friday", "Saturday", - }; - static const char months_l[][10] = { - "January", "February", "March", "April", - "May", "June", "July", "August", "September", - "October", "November", "December", - }; - static const char ampm[][3] = { "AM", "PM", }; + /* various tables for locale C */ + static const char days_l[][10] = { + "Sunday", "Monday", "Tuesday", "Wednesday", + "Thursday", "Friday", "Saturday", + }; + static const char months_l[][10] = { + "January", "February", "March", "April", + "May", "June", "July", "August", "September", + "October", "November", "December", + }; + static const char ampm[][3] = { "AM", "PM", }; - if (s == NULL || format == NULL || tmx == NULL || maxsize == 0) - return 0; + if (s == NULL || format == NULL || tmx == NULL || maxsize == 0) + return 0; - /* quick check if we even need to bother */ - if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) { - err: - errno = ERANGE; - return 0; - } + /* quick check if we even need to bother */ + if (strchr(format, '%') == NULL && strlen(format) + 1 >= maxsize) { + err: + errno = ERANGE; + return 0; + } - for (; *format && s < endp - 1; format++) { -#define FLAG_FOUND() do { \ - if (precision > 0 || flags & (BIT_OF(LOCALE_E)|BIT_OF(LOCALE_O))) \ - goto unknown; \ - } while (0) + for (; *format && s < endp - 1; format++) { +#define FLAG_FOUND() do { \ + if (precision > 0 || flags & (BIT_OF(LOCALE_E)|BIT_OF(LOCALE_O))) \ + goto unknown; \ + } while (0) #define NEEDS(n) do if (s >= endp || (n) >= endp - s - 1) goto err; while (0) -#define FILL_PADDING(i) do { \ - if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \ - NEEDS(precision); \ - memset(s, padding ? padding : ' ', precision - (i)); \ - s += precision - (i); \ - } \ - else { \ - NEEDS(i); \ - } \ -} while (0); -#define FMT(def_pad, def_prec, fmt, val) \ - do { \ - int l; \ - if (precision <= 0) precision = (def_prec); \ - if (flags & BIT_OF(LEFT)) precision = 1; \ - l = snprintf(s, endp - s, \ - ((padding == '0' || (!padding && (def_pad) == '0')) ? "%0*"fmt : "%*"fmt), \ - precision, (val)); \ - if (l < 0) goto err; \ - s += l; \ - } while (0) -#define STRFTIME(fmt) \ - do { \ - i = date_strftime_with_tmx(s, endp - s, (fmt), tmx); \ - if (!i) return 0; \ - if (precision > i) {\ - if (start + maxsize < s + precision) { \ - errno = ERANGE; \ - return 0; \ - } \ - memmove(s + precision - i, s, i);\ - memset(s, padding ? padding : ' ', precision - i); \ - s += precision; \ - }\ - else s += i; \ - } while (0) -#define FMTV(def_pad, def_prec, fmt, val) \ - do { \ - VALUE tmp = (val); \ - if (FIXNUM_P(tmp)) { \ - FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \ - } \ - else { \ - VALUE args[2], result; \ - size_t l; \ - if (precision <= 0) precision = (def_prec); \ - if (flags & BIT_OF(LEFT)) precision = 1; \ - args[0] = INT2FIX(precision); \ - args[1] = (val); \ - if (padding == '0' || (!padding && (def_pad) == '0')) \ - result = rb_str_format(2, args, rb_str_new2("%0*"fmt)); \ - else \ - result = rb_str_format(2, args, rb_str_new2("%*"fmt)); \ - l = strlcpy(s, StringValueCStr(result), endp-s); \ - if ((size_t)(endp-s) <= l) \ - goto err; \ - s += l; \ - } \ - } while (0) +#define FILL_PADDING(i) do { \ + if (!(flags & BIT_OF(LEFT)) && precision > (i)) { \ + NEEDS(precision); \ + memset(s, padding ? padding : ' ', precision - (i)); \ + s += precision - (i); \ + } \ + else { \ + NEEDS(i); \ + } \ + } while (0); +#define FMT(def_pad, def_prec, fmt, val) \ + do { \ + int l; \ + if (precision <= 0) precision = (def_prec); \ + if (flags & BIT_OF(LEFT)) precision = 1; \ + l = snprintf(s, endp - s, \ + ((padding == '0' || (!padding && (def_pad) == '0')) ? "%0*"fmt : "%*"fmt), \ + precision, (val)); \ + if (l < 0) goto err; \ + s += l; \ + } while (0) +#define STRFTIME(fmt) \ + do { \ + i = date_strftime_with_tmx(s, endp - s, (fmt), tmx); \ + if (!i) return 0; \ + if (precision > i) { \ + if (start + maxsize < s + precision) { \ + errno = ERANGE; \ + return 0; \ + } \ + memmove(s + precision - i, s, i); \ + memset(s, padding ? padding : ' ', precision - i); \ + s += precision; \ + } \ + else s += i; \ + } while (0) +#define FMTV(def_pad, def_prec, fmt, val) \ + do { \ + VALUE tmp = (val); \ + if (FIXNUM_P(tmp)) { \ + FMT((def_pad), (def_prec), "l"fmt, FIX2LONG(tmp)); \ + } \ + else { \ + VALUE args[2], result; \ + size_t l; \ + if (precision <= 0) precision = (def_prec); \ + if (flags & BIT_OF(LEFT)) precision = 1; \ + args[0] = INT2FIX(precision); \ + args[1] = (val); \ + if (padding == '0' || (!padding && (def_pad) == '0')) \ + result = rb_str_format(2, args, rb_str_new2("%0*"fmt)); \ + else \ + result = rb_str_format(2, args, rb_str_new2("%*"fmt)); \ + l = strlcpy(s, StringValueCStr(result), endp-s); \ + if ((size_t)(endp-s) <= l) \ + goto err; \ + s += l; \ + } \ + } while (0) - if (*format != '%') { - *s++ = *format; - continue; + if (*format != '%') { + *s++ = *format; + continue; + } + tp = tbuf; + sp = format; + precision = -1; + flags = 0; + padding = 0; + colons = 0; + again: + switch (*++format) { + case '\0': + format--; + goto unknown; + + case 'A': /* full weekday name */ + case 'a': /* abbreviated weekday name */ + if (flags & BIT_OF(CHCASE)) { + flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); + flags |= BIT_OF(UPPER); + } + { + int wday = tmx_wday; + if (wday < 0 || wday > 6) + i = 1, tp = "?"; + else { + if (*format == 'A') + i = strlen(tp = days_l[wday]); + else + i = 3, tp = days_l[wday]; } - tp = tbuf; - sp = format; - precision = -1; - flags = 0; - padding = 0; - colons = 0; - again: - switch (*++format) { - case '\0': - format--; - goto unknown; + } + break; - case 'A': /* full weekday name */ - case 'a': /* abbreviated weekday name */ - if (flags & BIT_OF(CHCASE)) { - flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); - flags |= BIT_OF(UPPER); - } - { - int wday = tmx_wday; - if (wday < 0 || wday > 6) - i = 1, tp = "?"; - else { - if (*format == 'A') - i = strlen(tp = days_l[wday]); - else - i = 3, tp = days_l[wday]; - } - } - break; + case 'B': /* full month name */ + case 'b': /* abbreviated month name */ + case 'h': /* abbreviated month name */ + if (flags & BIT_OF(CHCASE)) { + flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); + flags |= BIT_OF(UPPER); + } + { + int mon = tmx_mon; + if (mon < 1 || mon > 12) + i = 1, tp = "?"; + else { + if (*format == 'B') + i = strlen(tp = months_l[mon-1]); + else + i = 3, tp = months_l[mon-1]; + } + } + break; - case 'B': /* full month name */ - case 'b': /* abbreviated month name */ -#ifdef SYSV_EXT - case 'h': /* abbreviated month name */ -#endif - if (flags & BIT_OF(CHCASE)) { - flags &= ~(BIT_OF(LOWER)|BIT_OF(CHCASE)); - flags |= BIT_OF(UPPER); - } - { - int mon = tmx_mon; - if (mon < 1 || mon > 12) - i = 1, tp = "?"; - else { - if (*format == 'B') - i = strlen(tp = months_l[mon-1]); - else - i = 3, tp = months_l[mon-1]; - } - } - break; + case 'C': + FMTV('0', 2, "d", div(tmx_year, INT2FIX(100))); + continue; -#ifdef POSIX2_DATE - case 'C': - FMTV('0', 2, "d", div(tmx_year, INT2FIX(100))); - continue; -#endif + case 'c': /* appropriate date and time representation */ + STRFTIME("%a %b %e %H:%M:%S %Y"); + continue; - case 'c': /* appropriate date and time representation */ - STRFTIME("%a %b %e %H:%M:%S %Y"); - continue; + case 'D': /* date as %m/%d/%y */ + STRFTIME("%m/%d/%y"); + continue; -#ifdef SYSV_EXT - case 'D': /* date as %m/%d/%y */ - STRFTIME("%m/%d/%y"); - continue; -#endif + case 'd': /* day of the month, 01 - 31 */ + case 'e': /* day of month, blank padded */ + v = range(1, tmx_mday, 31); + FMT((*format == 'd') ? '0' : ' ', 2, "d", v); + continue; - case 'd': /* day of the month, 01 - 31 */ -#ifdef SYSV_EXT - case 'e': /* day of month, blank padded */ -#endif - v = range(1, tmx_mday, 31); - FMT((*format == 'd') ? '0' : ' ', 2, "d", v); - continue; + case 'F': /* Equivalent to %Y-%m-%d */ + STRFTIME("%Y-%m-%d"); + continue; - case 'F': /* Equivalent to %Y-%m-%d */ - STRFTIME("%Y-%m-%d"); - continue; + case 'G': /* year of ISO week with century */ + { + VALUE year = tmx_cwyear; + if (FIXNUM_P(year)) { + long y = FIX2LONG(year); + FMT('0', 0 <= y ? 4 : 5, "ld", y); + } + else { + FMTV('0', 4, "d", year); + } + } + continue; -#ifdef ISO_DATE_EXT - case 'G': /* year of ISO week with century */ - { - VALUE year = tmx_cwyear; - if (FIXNUM_P(year)) { - long y = FIX2LONG(year); - FMT('0', 0 <= y ? 4 : 5, "ld", y); - } - else { - FMTV('0', 4, "d", year); - } - } - continue; + case 'g': /* year of ISO week without a century */ + v = NUM2INT(mod(tmx_cwyear, INT2FIX(100))); + FMT('0', 2, "d", v); + continue; - case 'g': /* year of ISO week without a century */ - v = NUM2INT(mod(tmx_cwyear, INT2FIX(100))); - FMT('0', 2, "d", v); - continue; -#endif /* ISO_DATE_EXT */ + case 'H': /* hour, 24-hour clock, 00 - 23 */ + case 'k': /* hour, 24-hour clock, blank pad */ + v = range(0, tmx_hour, 23); + FMT((*format == 'H') ? '0' : ' ', 2, "d", v); + continue; - case 'H': /* hour, 24-hour clock, 00 - 23 */ -#ifdef SUNOS_EXT - case 'k': /* hour, 24-hour clock, blank pad */ -#endif - v = range(0, tmx_hour, 23); - FMT((*format == 'H') ? '0' : ' ', 2, "d", v); - continue; + case 'I': /* hour, 12-hour clock, 01 - 12 */ + case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */ + v = range(0, tmx_hour, 23); + if (v == 0) + v = 12; + else if (v > 12) + v -= 12; + FMT((*format == 'I') ? '0' : ' ', 2, "d", v); + continue; - case 'I': /* hour, 12-hour clock, 01 - 12 */ -#ifdef SUNOS_EXT - case 'l': /* hour, 12-hour clock, 1 - 12, blank pad */ -#endif - v = range(0, tmx_hour, 23); - if (v == 0) - v = 12; - else if (v > 12) - v -= 12; - FMT((*format == 'I') ? '0' : ' ', 2, "d", v); - continue; + case 'j': /* day of the year, 001 - 366 */ + v = range(1, tmx_yday, 366); + FMT('0', 3, "d", v); + continue; - case 'j': /* day of the year, 001 - 366 */ - v = range(1, tmx_yday, 366); - FMT('0', 3, "d", v); - continue; + case 'L': + w = 3; + goto subsec; - case 'L': - w = 3; - goto subsec; + case 'N': + /* + * fractional second digits. default is 9 digits + * (nanosecond). + * + * %3N millisecond (3 digits) + * %6N microsecond (6 digits) + * %9N nanosecond (9 digits) + */ + w = 9; + subsec: + if (precision <= 0) { + precision = w; + } + NEEDS(precision); - case 'N': - /* - * fractional second digits. default is 9 digits - * (nanosecond). - * - * %3N millisecond (3 digits) - * %6N microsecond (6 digits) - * %9N nanosecond (9 digits) - */ - w = 9; - subsec: - if (precision <= 0) { - precision = w; - } - NEEDS(precision); + { + VALUE subsec = tmx_sec_fraction; + int ww; + long n; - { - VALUE subsec = tmx_sec_fraction; - int ww; - long n; + ww = precision; + while (9 <= ww) { + subsec = mul(subsec, INT2FIX(1000000000)); + ww -= 9; + } + n = 1; + for (; 0 < ww; ww--) + n *= 10; + if (n != 1) + subsec = mul(subsec, INT2FIX(n)); + subsec = div(subsec, INT2FIX(1)); - ww = precision; - while (9 <= ww) { - subsec = mul(subsec, INT2FIX(1000000000)); - ww -= 9; - } - n = 1; - for (; 0 < ww; ww--) - n *= 10; - if (n != 1) - subsec = mul(subsec, INT2FIX(n)); - subsec = div(subsec, INT2FIX(1)); + if (FIXNUM_P(subsec)) { + (void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec)); + s += precision; + } + else { + VALUE args[2], result; + args[0] = INT2FIX(precision); + args[1] = subsec; + result = rb_str_format(2, args, rb_str_new2("%0*d")); + (void)strlcpy(s, StringValueCStr(result), endp-s); + s += precision; + } + } + continue; - if (FIXNUM_P(subsec)) { - (void)snprintf(s, endp - s, "%0*ld", precision, FIX2LONG(subsec)); - s += precision; - } - else { - VALUE args[2], result; - (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/