From eb093fd87d70b67f18f5fa9e978240bdb1a1af75 Mon Sep 17 00:00:00 2001 From: Kyle Gorman Date: Thu, 7 May 2026 18:17:36 -0700 Subject: [PATCH] Uses shared PrintField helper to reduce repetitiveness in `info` programs PiperOrigin-RevId: 912254486 --- openfst/extensions/far/info.h | 29 +++--- openfst/extensions/mpdt/BUILD.bazel | 1 + openfst/extensions/mpdt/info.h | 55 +++++------ openfst/extensions/pdt/BUILD.bazel | 1 + openfst/extensions/pdt/info.h | 38 +++----- openfst/lib/util.h | 8 ++ openfst/script/info-impl.cc | 144 ++++++++++------------------ 7 files changed, 109 insertions(+), 167 deletions(-) diff --git a/openfst/extensions/far/info.h b/openfst/extensions/far/info.h index 5924aa84..79a77988 100644 --- a/openfst/extensions/far/info.h +++ b/openfst/extensions/far/info.h @@ -30,12 +30,14 @@ #include #include "absl/log/log.h" +#include "absl/strings/str_join.h" #include "absl/strings/string_view.h" #include "absl/types/span.h" #include "openfst/extensions/far/far-reader.h" #include "openfst/extensions/far/far.h" #include "openfst/extensions/far/getters.h" #include "openfst/lib/fst.h" +#include "openfst/lib/util.h" namespace fst { @@ -112,24 +114,15 @@ void Info(absl::Span sources, absl::string_view begin_key, FarInfoData info; GetInfo(sources, begin_key, end_key, list_fsts, &info); if (!list_fsts) { - std::cout << std::left << std::setw(50) << "far type" << info.far_type - << '\n'; - std::cout << std::left << std::setw(50) << "arc type" << Arc::Type() - << std::endl; - std::cout << std::left << std::setw(50) << "fst type"; - for (auto iter = info.fst_types.begin(); iter != info.fst_types.end(); - ++iter) { - if (iter != info.fst_types.begin()) std::cout << ","; - std::cout << *iter; - } - std::cout << '\n'; - std::cout << std::left << std::setw(50) << "# of FSTs" << info.nfst << '\n'; - std::cout << std::left << std::setw(50) << "total # of states" - << info.nstate << '\n'; - std::cout << std::left << std::setw(50) << "total # of arcs" << info.narc - << '\n'; - std::cout << std::left << std::setw(50) << "total # of final states" - << info.nfinal << '\n'; + const auto old = std::cout.setf(std::ios::left); + PrintField(std::cout, "far type", info.far_type); + PrintField(std::cout, "arc type", Arc::Type()); + PrintField(std::cout, "fst type", absl::StrJoin(info.fst_types, ",")); + PrintField(std::cout, "# of FSTs", info.nfst); + PrintField(std::cout, "total # of states", info.nstate); + PrintField(std::cout, "total # of arcs", info.narc); + PrintField(std::cout, "total # of final states", info.nfinal); + std::cout.setf(old); } else { // FIXME(kbg): Grok, then document this. int wkey = 10; diff --git a/openfst/extensions/mpdt/BUILD.bazel b/openfst/extensions/mpdt/BUILD.bazel index f1ac4b25..39cc3386 100644 --- a/openfst/extensions/mpdt/BUILD.bazel +++ b/openfst/extensions/mpdt/BUILD.bazel @@ -118,6 +118,7 @@ cc_library( "//openfst/lib:util", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/strings", ], ) diff --git a/openfst/extensions/mpdt/info.h b/openfst/extensions/mpdt/info.h index 2442580d..545ed6b0 100644 --- a/openfst/extensions/mpdt/info.h +++ b/openfst/extensions/mpdt/info.h @@ -30,6 +30,7 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" +#include "absl/strings/str_cat.h" #include "openfst/lib/fst.h" #include "openfst/lib/util.h" @@ -167,37 +168,33 @@ MPdtInfo::MPdtInfo( template void MPdtInfo::Print() { const auto old = std::cout.setf(std::ios::left); - std::cout.width(50); - std::cout << "fst type" << FstType() << std::endl; - std::cout.width(50); - std::cout << "arc type" << ArcType() << std::endl; - std::cout.width(50); - std::cout << "# of states" << NumStates() << std::endl; - std::cout.width(50); - std::cout << "# of arcs" << NumArcs() << std::endl; - std::cout.width(50); - std::cout << "# of levels" << NumLevels() << std::endl; - std::cout.width(50); + PrintField(std::cout, "fst type", FstType()); + PrintField(std::cout, "arc type", ArcType()); + PrintField(std::cout, "# of states", NumStates()); + PrintField(std::cout, "# of arcs", NumArcs()); + PrintField(std::cout, "# of levels", NumLevels()); for (typename Arc::Label i = 0; i < nlevels; ++i) { int level = i + 1; - std::cout << "# of open parentheses at level " << level << "\t" - << NumOpenParens(i) << std::endl; - std::cout.width(50); - std::cout << "# of close parentheses at level " << level << "\t" - << NumCloseParens(i) << std::endl; - std::cout.width(50); - std::cout << "# of unique open parentheses at level " << level << "\t" - << NumUniqueOpenParens(i) << std::endl; - std::cout.width(50); - std::cout << "# of unique close parentheses at level " << level << "\t" - << NumUniqueCloseParens(i) << std::endl; - std::cout.width(50); - std::cout << "# of open parenthesis dest. states at level " << level << "\t" - << NumOpenParenStates(i) << std::endl; - std::cout.width(50); - std::cout << "# of close parenthesis source states at level " << level - << "\t" << NumCloseParenStates(i) << std::endl; - std::cout.width(50); + PrintField(std::cout, + absl::StrCat("# of open parentheses at level ", level), + NumOpenParens(i)); + PrintField(std::cout, + absl::StrCat("# of close parentheses at level ", level), + NumCloseParens(i)); + PrintField(std::cout, + absl::StrCat("# of unique open parentheses at level ", level), + NumUniqueOpenParens(i)); + PrintField(std::cout, + absl::StrCat("# of unique close parentheses at level ", level), + NumUniqueCloseParens(i)); + PrintField( + std::cout, + absl::StrCat("# of open parenthesis dest. states at level ", level), + NumOpenParenStates(i)); + PrintField( + std::cout, + absl::StrCat("# of close parenthesis source states at level ", level), + NumCloseParenStates(i)); } std::cout.setf(old); } diff --git a/openfst/extensions/pdt/BUILD.bazel b/openfst/extensions/pdt/BUILD.bazel index 4c284911..427f4327 100644 --- a/openfst/extensions/pdt/BUILD.bazel +++ b/openfst/extensions/pdt/BUILD.bazel @@ -159,6 +159,7 @@ cc_library( hdrs = ["info.h"], deps = [ "//openfst/lib", + "//openfst/lib:util", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/types:span", diff --git a/openfst/extensions/pdt/info.h b/openfst/extensions/pdt/info.h index 51ec0969..a6bdf884 100644 --- a/openfst/extensions/pdt/info.h +++ b/openfst/extensions/pdt/info.h @@ -31,6 +31,7 @@ #include "absl/container/flat_hash_set.h" #include "absl/types/span.h" #include "openfst/lib/fst.h" +#include "openfst/lib/util.h" namespace fst { @@ -137,30 +138,19 @@ PdtInfo::PdtInfo( template void PdtInfo::Print() { const auto old = std::cout.setf(std::ios::left); - std::cout.width(50); - std::cout << "fst type" << FstType() << std::endl; - std::cout.width(50); - std::cout << "arc type" << ArcType() << std::endl; - std::cout.width(50); - std::cout << "# of states" << NumStates() << std::endl; - std::cout.width(50); - std::cout << "# of arcs" << NumArcs() << std::endl; - std::cout.width(50); - std::cout << "# of open parentheses" << NumOpenParens() << std::endl; - std::cout.width(50); - std::cout << "# of close parentheses" << NumCloseParens() << std::endl; - std::cout.width(50); - std::cout << "# of unique open parentheses" << NumUniqueOpenParens() - << std::endl; - std::cout.width(50); - std::cout << "# of unique close parentheses" << NumUniqueCloseParens() - << std::endl; - std::cout.width(50); - std::cout << "# of open parenthesis dest. states" << NumOpenParenStates() - << std::endl; - std::cout.width(50); - std::cout << "# of close parenthesis source states" << NumCloseParenStates() - << std::endl; + PrintField(std::cout, "fst type", FstType()); + PrintField(std::cout, "arc type", ArcType()); + PrintField(std::cout, "# of states", NumStates()); + PrintField(std::cout, "# of arcs", NumArcs()); + PrintField(std::cout, "# of open parentheses", NumOpenParens()); + PrintField(std::cout, "# of close parentheses", NumCloseParens()); + PrintField(std::cout, "# of unique open parentheses", NumUniqueOpenParens()); + PrintField(std::cout, "# of unique close parentheses", + NumUniqueCloseParens()); + PrintField(std::cout, "# of open parenthesis dest. states", + NumOpenParenStates()); + PrintField(std::cout, "# of close parenthesis source states", + NumCloseParenStates()); std::cout.setf(old); } diff --git a/openfst/lib/util.h b/openfst/lib/util.h index 3fdd5489..1c6cbd33 100644 --- a/openfst/lib/util.h +++ b/openfst/lib/util.h @@ -438,6 +438,14 @@ void ConvertToLegalCSymbol(std::string* s); // Utilities for stream I/O. +// Helper template for printing labeled fields to output streams. +template +inline void PrintField(std::ostream& ostrm, absl::string_view label, + const T& value, int width = 50) { + ostrm.width(width); + ostrm << label << value << std::endl; +} + bool AlignInput(std::istream& strm, size_t align = MappedFile::kArchAlignment); bool AlignOutput(std::ostream& strm, size_t align = MappedFile::kArchAlignment); diff --git a/openfst/script/info-impl.cc b/openfst/script/info-impl.cc index d21b1c21..7bf9eff5 100644 --- a/openfst/script/info-impl.cc +++ b/openfst/script/info-impl.cc @@ -27,48 +27,31 @@ #include "absl/strings/string_view.h" #include "openfst/lib/fst.h" #include "openfst/lib/properties.h" +#include "openfst/lib/util.h" #include "openfst/script/arcfilter-impl.h" namespace fst { -// Column width for property names. -constexpr int kWidth = 50; - void FstInfo::Info() const { std::ostream& ostrm = std::cout; const auto old = ostrm.setf(std::ios::left); - ostrm.width(kWidth); - ostrm << "fst type" << FstType() << std::endl; - ostrm.width(kWidth); - ostrm << "arc type" << ArcType() << std::endl; - ostrm.width(kWidth); - ostrm << "input symbol table" << InputSymbols() << std::endl; - ostrm.width(kWidth); - ostrm << "output symbol table" << OutputSymbols() << std::endl; + PrintField(ostrm, "fst type", FstType()); + PrintField(ostrm, "arc type", ArcType()); + PrintField(ostrm, "input symbol table", InputSymbols()); + PrintField(ostrm, "output symbol table", OutputSymbols()); if (!LongInfo()) { ostrm.setf(old); return; } - ostrm.width(kWidth); - ostrm << "# of states" << NumStates() << std::endl; - ostrm.width(kWidth); - ostrm << "# of arcs" << NumArcs() << std::endl; - ostrm.width(kWidth); - ostrm << "initial state" << Start() << std::endl; - ostrm.width(kWidth); - ostrm << "# of final states" << NumFinal() << std::endl; - ostrm.width(kWidth); - ostrm << "# of input/output epsilons" << NumEpsilons() << std::endl; - ostrm.width(kWidth); - ostrm << "# of input epsilons" << NumInputEpsilons() << std::endl; - ostrm.width(kWidth); - ostrm << "# of output epsilons" << NumOutputEpsilons() << std::endl; - ostrm.width(kWidth); - ostrm << "input label multiplicity" << InputLabelMultiplicity() << std::endl; - ostrm.width(kWidth); - ostrm << "output label multiplicity" << OutputLabelMultiplicity() - << std::endl; - ostrm.width(kWidth); + PrintField(ostrm, "# of states", NumStates()); + PrintField(ostrm, "# of arcs", NumArcs()); + PrintField(ostrm, "initial state", Start()); + PrintField(ostrm, "# of final states", NumFinal()); + PrintField(ostrm, "# of input/output epsilons", NumEpsilons()); + PrintField(ostrm, "# of input epsilons", NumInputEpsilons()); + PrintField(ostrm, "# of output epsilons", NumOutputEpsilons()); + PrintField(ostrm, "input label multiplicity", InputLabelMultiplicity()); + PrintField(ostrm, "output label multiplicity", OutputLabelMultiplicity()); std::string arc_type = ""; switch (ArcFilterType()) { case script::ArcFilterType::ANY: @@ -86,42 +69,26 @@ void FstInfo::Info() const { break; } } - const auto accessible_label = - absl::StrCat("# of ", arc_type, "accessible states"); - ostrm.width(kWidth); - ostrm << accessible_label << NumAccessible() << std::endl; - const auto coaccessible_label = - absl::StrCat("# of ", arc_type, "coaccessible states"); - ostrm.width(kWidth); - ostrm << coaccessible_label << NumCoAccessible() << std::endl; - const auto connected_label = - absl::StrCat("# of ", arc_type, "connected states"); - ostrm.width(kWidth); - ostrm << connected_label << NumConnected() << std::endl; - const auto numcc_label = - absl::StrCat("# of ", arc_type, "connected components"); - ostrm.width(kWidth); - ostrm << numcc_label << NumCc() << std::endl; - const auto numscc_label = - absl::StrCat("# of ", arc_type, "strongly conn components"); - ostrm.width(kWidth); - ostrm << numscc_label << NumScc() << std::endl; - ostrm.width(kWidth); - ostrm << "input matcher" - << (InputMatchType() == MATCH_INPUT ? 'y' - : InputMatchType() == MATCH_NONE ? 'n' - : '?') - << std::endl; - ostrm.width(kWidth); - ostrm << "output matcher" - << (OutputMatchType() == MATCH_OUTPUT ? 'y' - : OutputMatchType() == MATCH_NONE ? 'n' - : '?') - << std::endl; - ostrm.width(kWidth); - ostrm << "input lookahead" << (InputLookAhead() ? 'y' : 'n') << std::endl; - ostrm.width(kWidth); - ostrm << "output lookahead" << (OutputLookAhead() ? 'y' : 'n') << std::endl; + PrintField(ostrm, absl::StrCat("# of ", arc_type, "accessible states"), + NumAccessible()); + PrintField(ostrm, absl::StrCat("# of ", arc_type, "coaccessible states"), + NumCoAccessible()); + PrintField(ostrm, absl::StrCat("# of ", arc_type, "connected states"), + NumConnected()); + PrintField(ostrm, absl::StrCat("# of ", arc_type, "connected components"), + NumCc()); + PrintField(ostrm, absl::StrCat("# of ", arc_type, "strongly conn components"), + NumScc()); + PrintField(ostrm, "input matcher", + (InputMatchType() == MATCH_INPUT ? 'y' + : InputMatchType() == MATCH_NONE ? 'n' + : '?')); + PrintField(ostrm, "output matcher", + (OutputMatchType() == MATCH_OUTPUT ? 'y' + : OutputMatchType() == MATCH_NONE ? 'n' + : '?')); + PrintField(ostrm, "input lookahead", (InputLookAhead() ? 'y' : 'n')); + PrintField(ostrm, "output lookahead", (OutputLookAhead() ? 'y' : 'n')); PrintProperties(ostrm, Properties()); ostrm.setf(old); } @@ -131,8 +98,7 @@ void PrintProperties(std::ostream& ostrm, const uint64_t properties) { for (auto i = 0; i < 64; ++i, prop <<= 1) { if (prop & kBinaryProperties) { const char value = properties & prop ? 'y' : 'n'; - ostrm.width(kWidth); - ostrm << internal::PropertyNames[i] << value << std::endl; + PrintField(ostrm, internal::PropertyNames[i], value); } else if (prop & kPosTrinaryProperties) { char value = '?'; if (properties & prop) { @@ -140,40 +106,26 @@ void PrintProperties(std::ostream& ostrm, const uint64_t properties) { } else if (properties & prop << 1) { value = 'n'; } - ostrm.width(kWidth); - ostrm << internal::PropertyNames[i] << value << std::endl; + PrintField(ostrm, internal::PropertyNames[i], value); } } } void PrintHeader(std::ostream& ostrm, const FstHeader& header) { const auto old = ostrm.setf(std::ios::left); - ostrm.width(kWidth); - ostrm << "fst type" << header.FstType() << std::endl; - ostrm.width(kWidth); - ostrm << "arc type" << header.ArcType() << std::endl; - ostrm.width(kWidth); - ostrm << "version" << header.Version() << std::endl; - - // Flags + PrintField(ostrm, "fst type", header.FstType()); + PrintField(ostrm, "arc type", header.ArcType()); + PrintField(ostrm, "version", header.Version()); + // Flags. const auto flags = header.GetFlags(); - ostrm.width(kWidth); - ostrm << "input symbol table" << (flags & FstHeader::HAS_ISYMBOLS ? 'y' : 'n') - << std::endl; - ostrm.width(kWidth); - ostrm << "output symbol table" - << (flags & FstHeader::HAS_OSYMBOLS ? 'y' : 'n') << std::endl; - ostrm.width(kWidth); - ostrm << "aligned" << (flags & FstHeader::IS_ALIGNED ? 'y' : 'n') - << std::endl; - - ostrm.width(kWidth); - ostrm << "initial state" << header.Start() << std::endl; - ostrm.width(kWidth); - ostrm << "# of states" << header.NumStates() << std::endl; - ostrm.width(kWidth); - ostrm << "# of arcs" << header.NumArcs() << std::endl; - + PrintField(ostrm, "input symbol table", + (flags & FstHeader::HAS_ISYMBOLS ? 'y' : 'n')); + PrintField(ostrm, "output symbol table", + (flags & FstHeader::HAS_OSYMBOLS ? 'y' : 'n')); + PrintField(ostrm, "aligned", (flags & FstHeader::IS_ALIGNED ? 'y' : 'n')); + PrintField(ostrm, "initial state", header.Start()); + PrintField(ostrm, "# of states", header.NumStates()); + PrintField(ostrm, "# of arcs", header.NumArcs()); PrintProperties(ostrm, header.Properties()); ostrm.setf(old); }