From 309efcfb19275175efcd14a14e26b0828298c6ee Mon Sep 17 00:00:00 2001 From: litlighilit Date: Thu, 13 Jun 2024 21:45:23 +0800 Subject: [PATCH] fix(py-ver): str.capitalize still use uppercase for 1st char before py3.8 --- src/pylib/pystring/strmeth.nim | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/pylib/pystring/strmeth.nim b/src/pylib/pystring/strmeth.nim index 6a86bdad1..a7bf0ef39 100644 --- a/src/pylib/pystring/strmeth.nim +++ b/src/pylib/pystring/strmeth.nim @@ -7,6 +7,7 @@ import ./strimpl import ./strip, ./split/[split, rsplit] export strip, split, rsplit import ../stringlib/meth +import ../version include ./unicase/toUpperMapper const OneUpperToMoreTable = toTable OneUpperToMoreTableLit @@ -118,7 +119,9 @@ func title*(a: PyStr): PyStr = result = str res func capitalize*(a: PyStr): PyStr = - ## make the first character have title case and the rest lower case. + ## make the first character have title/upper case and the rest lower case. + ## + ## changed when Python 3.8: the first character will have title case. ## ## while Nim's `unicode.capitalize` only make the first character upper-case. let s = $a @@ -128,7 +131,11 @@ func capitalize*(a: PyStr): PyStr = rune: Rune i = 0 fastRuneAt(s, i, rune, doInc = true) - result = $py_toTitle(rune) + substr(s, i).lower() + let first = when (PyMajor, PyMinor) < (3,8): + py_toUpper(rune) + else: + py_toTitle(rune) + result = $first + substr(s, i).lower() export strutils.startsWith, strutils.endsWith