All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] apply: check date of potential epoch timestamps first
@ 2017-08-25 19:04 René Scharfe
  2017-08-25 19:06 ` [PATCH 2/2] apply: remove epoch date from regex René Scharfe
  0 siblings, 1 reply; 2+ messages in thread
From: René Scharfe @ 2017-08-25 19:04 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

has_epoch_timestamp() looks for time stamps that amount to either
1969-12-31 24:00 or 1970-01-01 00:00 after applying the time zone
offset.  Move the check for these two dates up, set the expected hour
based on which one is found, or exit early if none of them are present,
thus avoiding to engage the regex machinery for newer dates.

This also gets rid of two magic string length constants.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 apply.c | 33 +++++++++++++++++----------------
 1 file changed, 17 insertions(+), 16 deletions(-)

diff --git a/apply.c b/apply.c
index 956f56a927..e14077eaee 100644
--- a/apply.c
+++ b/apply.c
@@ -819,8 +819,7 @@ static int has_epoch_timestamp(const char *nameline)
 	const char *timestamp = NULL, *cp, *colon;
 	static regex_t *stamp;
 	regmatch_t m[10];
-	int zoneoffset;
-	int hourminute;
+	int zoneoffset, epoch_hour, hour, minute;
 	int status;
 
 	for (cp = nameline; *cp != '\n'; cp++) {
@@ -829,6 +828,18 @@ static int has_epoch_timestamp(const char *nameline)
 	}
 	if (!timestamp)
 		return 0;
+
+	/*
+	 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
+	 * (west of GMT) or 1970-01-01 (east of GMT)
+	 */
+	if (starts_with(timestamp, "1969-12-31"))
+		epoch_hour = 24;
+	else if (starts_with(timestamp, "1970-01-01"))
+		epoch_hour = 0;
+	else
+		return 0;
+
 	if (!stamp) {
 		stamp = xmalloc(sizeof(*stamp));
 		if (regcomp(stamp, stamp_regexp, REG_EXTENDED)) {
@@ -846,6 +857,9 @@ static int has_epoch_timestamp(const char *nameline)
 		return 0;
 	}
 
+	hour = strtol(timestamp + 11, NULL, 10);
+	minute = strtol(timestamp + 14, NULL, 10);
+
 	zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
 	if (*colon == ':')
 		zoneoffset = zoneoffset * 60 + strtol(colon + 1, NULL, 10);
@@ -854,20 +868,7 @@ static int has_epoch_timestamp(const char *nameline)
 	if (timestamp[m[3].rm_so] == '-')
 		zoneoffset = -zoneoffset;
 
-	/*
-	 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
-	 * (west of GMT) or 1970-01-01 (east of GMT)
-	 */
-	if ((zoneoffset < 0 && memcmp(timestamp, "1969-12-31", 10)) ||
-	    (0 <= zoneoffset && memcmp(timestamp, "1970-01-01", 10)))
-		return 0;
-
-	hourminute = (strtol(timestamp + 11, NULL, 10) * 60 +
-		      strtol(timestamp + 14, NULL, 10) -
-		      zoneoffset);
-
-	return ((zoneoffset < 0 && hourminute == 1440) ||
-		(0 <= zoneoffset && !hourminute));
+	return hour * 60 + minute - zoneoffset == epoch_hour * 60;
 }
 
 /*
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] apply: remove epoch date from regex
  2017-08-25 19:04 [PATCH 1/2] apply: check date of potential epoch timestamps first René Scharfe
@ 2017-08-25 19:06 ` René Scharfe
  0 siblings, 0 replies; 2+ messages in thread
From: René Scharfe @ 2017-08-25 19:06 UTC (permalink / raw)
  To: Git List; +Cc: Junio C Hamano

We check the date of epoch timestamp candidates already with
starts_with().  Move beyond that part using skip_prefix() instead of
checking it again using a regular expression.  Also group the minutes
part, so that we can access them using a substring match instead of
using a magic number.

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 apply.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/apply.c b/apply.c
index e14077eaee..9c70b4040a 100644
--- a/apply.c
+++ b/apply.c
@@ -811,9 +811,7 @@ static int has_epoch_timestamp(const char *nameline)
 	 * 1970-01-01, and the seconds part must be "00".
 	 */
 	const char stamp_regexp[] =
-		"^(1969-12-31|1970-01-01)"
-		" "
-		"[0-2][0-9]:[0-5][0-9]:00(\\.0+)?"
+		"^[0-2][0-9]:([0-5][0-9]):00(\\.0+)?"
 		" "
 		"([-+][0-2][0-9]:?[0-5][0-9])\n";
 	const char *timestamp = NULL, *cp, *colon;
@@ -833,9 +831,9 @@ static int has_epoch_timestamp(const char *nameline)
 	 * YYYY-MM-DD hh:mm:ss must be from either 1969-12-31
 	 * (west of GMT) or 1970-01-01 (east of GMT)
 	 */
-	if (starts_with(timestamp, "1969-12-31"))
+	if (skip_prefix(timestamp, "1969-12-31 ", &timestamp))
 		epoch_hour = 24;
-	else if (starts_with(timestamp, "1970-01-01"))
+	else if (skip_prefix(timestamp, "1970-01-01 ", &timestamp))
 		epoch_hour = 0;
 	else
 		return 0;
@@ -857,8 +855,8 @@ static int has_epoch_timestamp(const char *nameline)
 		return 0;
 	}
 
-	hour = strtol(timestamp + 11, NULL, 10);
-	minute = strtol(timestamp + 14, NULL, 10);
+	hour = strtol(timestamp, NULL, 10);
+	minute = strtol(timestamp + m[1].rm_so, NULL, 10);
 
 	zoneoffset = strtol(timestamp + m[3].rm_so + 1, (char **) &colon, 10);
 	if (*colon == ':')
-- 
2.14.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-08-25 19:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-25 19:04 [PATCH 1/2] apply: check date of potential epoch timestamps first René Scharfe
2017-08-25 19:06 ` [PATCH 2/2] apply: remove epoch date from regex René Scharfe

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.