From 3099b07c441dec089a1675f3b0aad73b9b72944a Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Mon, 7 Mar 2016 12:07:06 -0500 Subject: [PATCH] lib/core/text: move `to_base` to the main base module and remove the useless `signed` parameter Signed-off-by: Jean Privat --- lib/core/text/abstract_text.nit | 30 +++++++++++++++++++++++------- lib/core/text/flat.nit | 8 -------- src/doc/doc_phases/doc_console.nit | 2 +- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/core/text/abstract_text.nit b/lib/core/text/abstract_text.nit index 405eb76..859808a 100644 --- a/lib/core/text/abstract_text.nit +++ b/lib/core/text/abstract_text.nit @@ -615,7 +615,7 @@ abstract class Text b.append("\\\\") else if c.code_point < 32 then b.add('\\') - var oct = c.code_point.to_base(8, false) + var oct = c.code_point.to_base(8) # Force 3 octal digits since it is the # maximum allowed in the C specification if oct.length == 1 then @@ -689,7 +689,7 @@ abstract class Text b.add('\\') b.add(c) else if c.code_point < 32 or c == ';' or c == '|' or c == '\\' or c == '=' then - b.append("?{c.code_point.to_base(16, false)}") + b.append("?{c.code_point.to_base(16)}") else b.add(c) end @@ -1569,9 +1569,9 @@ redef class Int # Returns a string describing error number fun strerror: String do return strerror_ext.to_s - # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if 'signed' and negative). + # Fill `s` with the digits in base `base` of `self` (and with the '-' sign if negative). # assume < to_c max const of char - private fun fill_buffer(s: Buffer, base: Int, signed: Bool) + private fun fill_buffer(s: Buffer, base: Int) do var n: Int # Sign @@ -1603,14 +1603,30 @@ redef class Int snprintf(nstr, strlen, "%ld", self); `} - # return displayable int in base base and signed - fun to_base(base: Int, signed: Bool): String is abstract + # String representation of `self` in the given `base` + # + # ~~~ + # assert 15.to_base(10) == "15" + # assert 15.to_base(16) == "f" + # assert 15.to_base(2) == "1111" + # assert (-10).to_base(3) == "-101" + # ~~~ + fun to_base(base: Int): String + do + var l = digit_count(base) + var s = new Buffer + s.enlarge(l) + for x in [0..l[ do s.add(' ') + fill_buffer(s, base) + return s.to_s + end + # return displayable int in hexadecimal # # assert 1.to_hex == "1" # assert (-255).to_hex == "-ff" - fun to_hex: String do return to_base(16,false) + fun to_hex: String do return to_base(16) end redef class Float diff --git a/lib/core/text/flat.nit b/lib/core/text/flat.nit index 8081ecc..18fdabe 100644 --- a/lib/core/text/flat.nit +++ b/lib/core/text/flat.nit @@ -1330,14 +1330,6 @@ redef class NativeString end redef class Int - redef fun to_base(base, signed) - do - var l = digit_count(base) - var s = new FlatBuffer.from(" " * l) - fill_buffer(s, base, signed) - return s.to_s - end - # return displayable int in base 10 and signed # # assert 1.to_s == "1" diff --git a/src/doc/doc_phases/doc_console.nit b/src/doc/doc_phases/doc_console.nit index 070b86b..56fbc74 100644 --- a/src/doc/doc_phases/doc_console.nit +++ b/src/doc/doc_phases/doc_console.nit @@ -551,7 +551,7 @@ private class Pager else if c == '`' then b.append("'") else if c.code_point < 32 then - b.append("\\{c.code_point.to_base(8, false)}") + b.append("\\{c.code_point.to_base(8)}") else b.add(c) end -- 1.7.9.5