Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
ece58deb
Kaydet (Commit)
ece58deb
authored
Nis 23, 2012
tarafından
Victor Stinner
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Close #14648: Compute correctly maxchar in str.format() for substrin
üst
0b7d7c95
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
50 additions
and
6 deletions
+50
-6
unicodeobject.h
Include/unicodeobject.h
+9
-0
test_unicode.py
Lib/test/test_unicode.py
+8
-2
unicodeobject.c
Objects/unicodeobject.c
+31
-0
formatter_unicode.c
Python/formatter_unicode.c
+2
-4
No files found.
Include/unicodeobject.h
Dosyayı görüntüle @
ece58deb
...
@@ -710,6 +710,15 @@ PyAPI_FUNC(PyObject*) PyUnicode_Substring(
...
@@ -710,6 +710,15 @@ PyAPI_FUNC(PyObject*) PyUnicode_Substring(
Py_ssize_t
start
,
Py_ssize_t
start
,
Py_ssize_t
end
);
Py_ssize_t
end
);
#ifndef Py_LIMITED_API
/* Compute the maximum character of the substring unicode[start:end].
Return 127 for an empty string. */
PyAPI_FUNC
(
Py_UCS4
)
_PyUnicode_FindMaxChar
(
PyObject
*
unicode
,
Py_ssize_t
start
,
Py_ssize_t
end
);
#endif
/* Copy the string into a UCS4 buffer including the null character if copy_null
/* Copy the string into a UCS4 buffer including the null character if copy_null
is set. Return NULL and raise an exception on error. Raise a ValueError if
is set. Return NULL and raise an exception on error. Raise a ValueError if
the buffer is smaller than the string. Return buffer on success.
the buffer is smaller than the string. Return buffer on success.
...
...
Lib/test/test_unicode.py
Dosyayı görüntüle @
ece58deb
...
@@ -924,6 +924,14 @@ class UnicodeTest(string_tests.CommonTest,
...
@@ -924,6 +924,14 @@ class UnicodeTest(string_tests.CommonTest,
self
.
assertRaises
(
ValueError
,
format
,
''
,
'#'
)
self
.
assertRaises
(
ValueError
,
format
,
''
,
'#'
)
self
.
assertRaises
(
ValueError
,
format
,
''
,
'#20'
)
self
.
assertRaises
(
ValueError
,
format
,
''
,
'#20'
)
# Non-ASCII
self
.
assertEqual
(
"{0:s}{1:s}"
.
format
(
"ABC"
,
"
\u0410\u0411\u0412
"
),
'ABC
\u0410\u0411\u0412
'
)
self
.
assertEqual
(
"{0:.3s}"
.
format
(
"ABC
\u0410\u0411\u0412
"
),
'ABC'
)
self
.
assertEqual
(
"{0:.0s}"
.
format
(
"ABC
\u0410\u0411\u0412
"
),
''
)
def
test_format_map
(
self
):
def
test_format_map
(
self
):
self
.
assertEqual
(
''
.
format_map
({}),
''
)
self
.
assertEqual
(
''
.
format_map
({}),
''
)
self
.
assertEqual
(
'a'
.
format_map
({}),
'a'
)
self
.
assertEqual
(
'a'
.
format_map
({}),
'a'
)
...
@@ -1056,8 +1064,6 @@ class UnicodeTest(string_tests.CommonTest,
...
@@ -1056,8 +1064,6 @@ class UnicodeTest(string_tests.CommonTest,
self
.
assertEqual
(
'
%
f'
%
INF
,
'inf'
)
self
.
assertEqual
(
'
%
f'
%
INF
,
'inf'
)
self
.
assertEqual
(
'
%
F'
%
INF
,
'INF'
)
self
.
assertEqual
(
'
%
F'
%
INF
,
'INF'
)
self
.
assertEqual
(
format
(
"
\u0410\u0411\u0412
"
,
"s"
),
"АБВ"
)
def
test_startswith_endswith_errors
(
self
):
def
test_startswith_endswith_errors
(
self
):
for
meth
in
(
'foo'
.
startswith
,
'foo'
.
endswith
):
for
meth
in
(
'foo'
.
startswith
,
'foo'
.
endswith
):
with
self
.
assertRaises
(
TypeError
)
as
cm
:
with
self
.
assertRaises
(
TypeError
)
as
cm
:
...
...
Objects/unicodeobject.c
Dosyayı görüntüle @
ece58deb
...
@@ -1957,6 +1957,37 @@ PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
...
@@ -1957,6 +1957,37 @@ PyUnicode_FromKindAndData(int kind, const void *buffer, Py_ssize_t size)
}
}
}
}
Py_UCS4
_PyUnicode_FindMaxChar(PyObject *unicode, Py_ssize_t start, Py_ssize_t end)
{
enum PyUnicode_Kind kind;
void *startptr, *endptr;
assert(PyUnicode_IS_READY(unicode));
assert(0 <= start);
assert(end <= PyUnicode_GET_LENGTH(unicode));
assert(start <= end);
if (start == 0 && end == PyUnicode_GET_LENGTH(unicode))
return PyUnicode_MAX_CHAR_VALUE(unicode);
if (start == end)
return 127;
kind = PyUnicode_KIND(unicode);
startptr = PyUnicode_DATA(unicode);
endptr = (char*)startptr + end * kind;
if (start)
startptr = (char*)startptr + start * kind;
switch(kind)
{
case PyUnicode_1BYTE_KIND: return ucs1lib_find_max_char(startptr, endptr);
case PyUnicode_2BYTE_KIND: return ucs2lib_find_max_char(startptr, endptr);
default:
case PyUnicode_4BYTE_KIND: return ucs4lib_find_max_char(startptr, endptr);
}
}
/* Ensure that a string uses the most efficient storage, if it is not the
/* Ensure that a string uses the most efficient storage, if it is not the
case: create a new string with of the right kind. Write NULL into *p_unicode
case: create a new string with of the right kind. Write NULL into *p_unicode
on error. */
on error. */
...
...
Python/formatter_unicode.c
Dosyayı görüntüle @
ece58deb
...
@@ -716,7 +716,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format)
...
@@ -716,7 +716,7 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format)
Py_ssize_t
pos
;
Py_ssize_t
pos
;
Py_ssize_t
len
=
PyUnicode_GET_LENGTH
(
value
);
Py_ssize_t
len
=
PyUnicode_GET_LENGTH
(
value
);
PyObject
*
result
=
NULL
;
PyObject
*
result
=
NULL
;
Py_UCS4
maxchar
=
127
;
Py_UCS4
maxchar
;
/* sign is not allowed on strings */
/* sign is not allowed on strings */
if
(
format
->
sign
!=
'\0'
)
{
if
(
format
->
sign
!=
'\0'
)
{
...
@@ -747,11 +747,9 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format)
...
@@ -747,11 +747,9 @@ format_string_internal(PyObject *value, const InternalFormatSpec *format)
len
=
format
->
precision
;
len
=
format
->
precision
;
}
}
if
(
len
)
maxchar
=
PyUnicode_MAX_CHAR_VALUE
(
value
);
calc_padding
(
len
,
format
->
width
,
format
->
align
,
&
lpad
,
&
rpad
,
&
total
);
calc_padding
(
len
,
format
->
width
,
format
->
align
,
&
lpad
,
&
rpad
,
&
total
);
maxchar
=
_PyUnicode_FindMaxChar
(
value
,
0
,
len
);
if
(
lpad
!=
0
||
rpad
!=
0
)
if
(
lpad
!=
0
||
rpad
!=
0
)
maxchar
=
Py_MAX
(
maxchar
,
format
->
fill_char
);
maxchar
=
Py_MAX
(
maxchar
,
format
->
fill_char
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment