[tz] [PROPOSED 2/7] Preserve columns more nicely in vanguard form

Paul Eggert eggert at cs.ucla.edu
Wed Oct 26 04:10:16 UTC 2022


When converting to and from vanguard form, line up the columns in
‘backward’ more nicely.  This does not change the data’s meaning;
it merely inserts and deletes tabs.
* backward: Revert yesterday’s formatting changes, restoring the
file’s more nicely-indented columns.
* ziguard.awk (make_linkline): New function, which lines up columns.
(/^Link/, cut_link_chains_short): Use it.
---
 backward    | 10 +++++-----
 ziguard.awk | 49 ++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 47 insertions(+), 12 deletions(-)

diff --git a/backward b/backward
index 46dc17d..4c1c5d5 100644
--- a/backward
+++ b/backward
@@ -89,7 +89,7 @@ Link	Etc/GMT			GMT0
 Link	Etc/GMT			Greenwich
 # End of rearguard section.
 Link	Asia/Hong_Kong		Hongkong
-Link	Africa/Abidjan	Iceland	#= Atlantic/Reykjavik
+Link	Africa/Abidjan		Iceland	#= Atlantic/Reykjavik
 Link	Asia/Tehran		Iran
 Link	Asia/Jerusalem		Israel
 Link	America/Jamaica		Jamaica
@@ -101,7 +101,7 @@ Link	America/Mazatlan	Mexico/BajaSur
 Link	America/Mexico_City	Mexico/General
 Link	Pacific/Auckland	NZ
 Link	Pacific/Chatham		NZ-CHAT
-Link	America/Denver	Navajo	#= America/Shiprock
+Link	America/Denver		Navajo	#= America/Shiprock
 Link	Asia/Shanghai		PRC
 Link	Europe/Warsaw		Poland
 Link	Europe/Lisbon		Portugal
@@ -139,7 +139,7 @@ Link	America/Argentina/Jujuy		America/Jujuy
 Link	America/Indiana/Knox		America/Knox_IN
 Link	America/Kentucky/Louisville	America/Louisville
 Link	America/Argentina/Mendoza	America/Mendoza
-Link	America/Puerto_Rico	America/Virgin	#= America/St_Thomas
+Link	America/Puerto_Rico		America/Virgin	#= America/St_Thomas
 Link	Pacific/Pago_Pago		Pacific/Samoa
 
 
@@ -293,11 +293,11 @@ Link	Pacific/Port_Moresby	Pacific/Yap
 # Alternate names for the same location
 
 # Link	TARGET			LINK-NAME	#= TARGET1
-Link	Africa/Nairobi	Africa/Asmera	#= Africa/Asmara
+Link	Africa/Nairobi		Africa/Asmera	#= Africa/Asmara
 Link	America/Nuuk		America/Godthab
 Link	Asia/Ashgabat		Asia/Ashkhabad
 Link	Asia/Kolkata		Asia/Calcutta
-Link	Asia/Shanghai	Asia/Chungking	#= Asia/Chongqing
+Link	Asia/Shanghai		Asia/Chungking	#= Asia/Chongqing
 Link	Asia/Dhaka		Asia/Dacca
 # Istanbul is in both continents.
 Link	Europe/Istanbul		Asia/Istanbul
diff --git a/ziguard.awk b/ziguard.awk
index 8cff035..7a3404f 100644
--- a/ziguard.awk
+++ b/ziguard.awk
@@ -311,8 +311,37 @@ DATAFORM != "main" {
   }
 }
 
+# Return a link line resulting by changing OLDLINE to link to TARGET
+# from LINKNAME, instead of linking to OLDTARGET from LINKNAME.
+# Align data columns the same as they were in OLDLINE.
+# Also, replace any existing white space followed by comment with COMMENT.
+function make_linkline(oldline, target, linkname, oldtarget, comment, \
+		       oldprefix, oldprefixlen, oldtargettabs, \
+		       replsuffix, targettabs)
+{
+  oldprefix = "Link\t" oldtarget "\t"
+  oldprefixlen = length(oldprefix)
+  if (substr(oldline, 1, oldprefixlen) == oldprefix) {
+    # Use tab stops to preserve LINKNAME's column.
+    replsuffix = substr(oldline, oldprefixlen + 1)
+    sub(/[\t ]*#.*/, "", replsuffix)
+    oldtargettabs = int(length(oldtarget) / 8) + 1
+    targettabs = int(length(target) / 8) + 1
+    for (; targettabs < oldtargettabs; targettabs++) {
+      replsuffix = "\t" replsuffix
+    }
+    for (; oldtargettabs < targettabs && replsuffix ~ /^\t/; targettabs--) {
+      replsuffix = substr(replsuffix, 2)
+    }
+  } else {
+    # Odd format line; don't bother lining up its replacement nicely.
+    replsuffix = linkname
+  }
+  return "Link\t" target "\t" replsuffix comment
+}
+
 /^Link/ && $4 == "#=" && DATAFORM == "vanguard" {
-  $0 = "Link\t" $5 "\t" $3
+  $0 = make_linkline($0, $5, $3, $2)
 }
 
 # If a Link line is followed by a Link or Zone line for the same data, comment
@@ -329,15 +358,21 @@ DATAFORM != "main" {
 
 { line[NR] = $0 }
 
-function cut_link_chains_short(\
-			       linkname, target, t, u)
+function cut_link_chains_short( \
+			       l, linkname, t, target)
 {
   for (linkname in linktarget) {
     target = linktarget[linkname]
-    for (t = target; (u = linktarget[t]); t = u)
-      continue;
-    if (t != target) {
-      line[linkline[linkname]] = "Link\t" t "\t" linkname "\t#= " target
+    t = linktarget[target]
+    if (t) {
+      # TARGET is itself a link name.  Replace the line "Link TARGET LINKNAME"
+      # with "Link T LINKNAME #= TARGET", where T is at the end of the chain
+      # of links that LINKNAME points to.
+      while ((u = linktarget[t])) {
+	t = u
+      }
+      l = linkline[linkname]
+      line[l] = make_linkline(line[l], t, linkname, target, "\t#= " target)
     }
   }
 }
-- 
2.37.3




More information about the tz mailing list