From e9f515fc9ba1a1e4be8b37313c59fcf32529322f Mon Sep 17 00:00:00 2001
From: lumidify <nobody@lumidify.org>
Date: Mon, 14 Nov 2022 19:00:22 +0100
Subject: [PATCH] Replace line number padding char with string

---
 lib/srchilite/linenumgenerator.cpp | 11 +++++++++--
 lib/srchilite/linenumgenerator.h   |  4 ++--
 lib/srchilite/sourcehighlight.cpp  |  2 +-
 lib/srchilite/sourcehighlight.h    |  6 +++---
 src/source-highlight.cc            |  2 +-
 5 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lib/srchilite/linenumgenerator.cpp b/lib/srchilite/linenumgenerator.cpp
index 26674e4..0eb3f02 100644
--- a/lib/srchilite/linenumgenerator.cpp
+++ b/lib/srchilite/linenumgenerator.cpp
@@ -21,7 +21,7 @@ namespace srchilite {
 static SubstitutionMapping substitutionmapping;
 
 LineNumGenerator::LineNumGenerator(const TextStyle &_lineStyle,
-        unsigned int _digitNum, char _padding) :
+        unsigned int _digitNum, std::string _padding) :
     digitNum(_digitNum), padding(_padding), lineStyle(_lineStyle) {
 }
 
@@ -31,7 +31,14 @@ LineNumGenerator::~LineNumGenerator() {
 const std::string LineNumGenerator::generateLine(unsigned int line) {
     ostringstream sout;
 
-    sout << std::setw(digitNum) << std::setfill(padding) << line << ":";
+    unsigned int tmpline = line;
+    unsigned int len = 1;
+    while (tmpline /= 10)
+        len++;
+    len = digitNum > len ? digitNum - len : 0;
+    while (len-- > 0)
+        sout << padding;
+    sout << line << ":";
 
     string line_str = lineStyle.output(sout.str());
 
diff --git a/lib/srchilite/linenumgenerator.h b/lib/srchilite/linenumgenerator.h
index c37acb7..26cc0db 100644
--- a/lib/srchilite/linenumgenerator.h
+++ b/lib/srchilite/linenumgenerator.h
@@ -21,7 +21,7 @@ class LineNumGenerator {
     unsigned int digitNum;
 
     /// character to use for padding the line number
-    char padding;
+    std::string padding;
 
     /// for actually formatting the line number
     TextStyle lineStyle;
@@ -33,7 +33,7 @@ class LineNumGenerator {
     std::string anchorLinePrefix;
 
 public:
-    LineNumGenerator(const TextStyle &lineStyle, unsigned int digitNum, char padding = '0');
+    LineNumGenerator(const TextStyle &lineStyle, unsigned int digitNum, std::string padding = "0");
     ~LineNumGenerator();
 
     void setAnchorStyle(const TextStyle &_anchorStyle) {
diff --git a/lib/srchilite/sourcehighlight.cpp b/lib/srchilite/sourcehighlight.cpp
index 11b6316..2031ac1 100644
--- a/lib/srchilite/sourcehighlight.cpp
+++ b/lib/srchilite/sourcehighlight.cpp
@@ -50,7 +50,7 @@ SourceHighlight::SourceHighlight(const std::string &_outputLang) :
             lineNumGenerator(0), docGenerator(0), noDocGenerator(0),
             highlightEventListener(0), ctagsManager(0), ctagsFormatter(0),
             lineRanges(0), regexRanges(0), optimize(true), generateLineNumbers(
-                    false), generateLineNumberRefs(false), lineNumberPad('0'),
+                    false), generateLineNumberRefs(false), lineNumberPad("0"),
             lineNumberDigits(0), generateEntireDoc(false),
             generateVersion(true), canUseStdOut(true), binaryOutput(false),
             tabSpaces(0) {
diff --git a/lib/srchilite/sourcehighlight.h b/lib/srchilite/sourcehighlight.h
index 4cef31e..33224f4 100644
--- a/lib/srchilite/sourcehighlight.h
+++ b/lib/srchilite/sourcehighlight.h
@@ -141,7 +141,7 @@ class SourceHighlight {
     std::string lineNumberAnchorPrefix;
 
     /// the line number padding char (default '0')
-    char lineNumberPad;
+    std::string lineNumberPad;
 
     /**
      * the number of digits for line numbers (if not specified this is
@@ -312,8 +312,8 @@ public:
         generateLineNumberRefs = b;
     }
 
-    void setLineNumberPad(char c) {
-        lineNumberPad = c;
+    void setLineNumberPad(std::string s) {
+        lineNumberPad = s;
     }
 
     void setLineNumberAnchorPrefix(const std::string &_prefix) {
diff --git a/src/source-highlight.cc b/src/source-highlight.cc
index 4feba30..7e12c46 100644
--- a/src/source-highlight.cc
+++ b/src/source-highlight.cc
@@ -352,7 +352,7 @@ int main(int argc, char * argv[]) {
 
         if (args_info.line_number_given) {
             // set the line number padding char (default is always '0')
-            sourcehighlight.setLineNumberPad(args_info.line_number_arg[0]);
+            sourcehighlight.setLineNumberPad(args_info.line_number_arg);
         }
 
         if (args_info.line_number_ref_given) {
-- 
2.30.2

