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
1f68fc7f
Kaydet (Commit)
1f68fc7f
authored
Haz 14, 2002
tarafından
Neal Norwitz
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
SF bug # 493951 string.{starts,ends}with vs slices
Handle negative indices similar to slices.
üst
585775bf
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
70 additions
and
45 deletions
+70
-45
whatsnew23.tex
Doc/whatsnew/whatsnew23.tex
+4
-0
string_tests.py
Lib/test/string_tests.py
+27
-0
NEWS
Misc/NEWS
+3
-0
stringobject.c
Objects/stringobject.c
+36
-45
No files found.
Doc/whatsnew/whatsnew23.tex
Dosyayı görüntüle @
1f68fc7f
...
...
@@ -497,6 +497,10 @@ u'\u4001abc'
>>>
\end{verbatim}
\item
The
\method
{
startswith()
}
and
\method
{
endswith()
}
string methods now have accept negative numbers for
start and end parameters.
\item
Another new string method is
\method
{
zfill()
}
, originally a
function in the
\module
{
string
}
module.
\method
{
zfill()
}
pads a
numeric string with zeros on the left until it's the specified width.
...
...
Lib/test/string_tests.py
Dosyayı görüntüle @
1f68fc7f
...
...
@@ -223,6 +223,18 @@ def run_method_tests(test):
test
(
'startswith'
,
'helloworld'
,
1
,
'lowo'
,
3
,
7
)
test
(
'startswith'
,
'helloworld'
,
0
,
'lowo'
,
3
,
6
)
# test negative indices in startswith
test
(
'startswith'
,
'hello'
,
1
,
'he'
,
0
,
-
1
)
test
(
'startswith'
,
'hello'
,
1
,
'he'
,
-
53
,
-
1
)
test
(
'startswith'
,
'hello'
,
0
,
'hello'
,
0
,
-
1
)
test
(
'startswith'
,
'hello'
,
0
,
'hello world'
,
-
1
,
-
10
)
test
(
'startswith'
,
'hello'
,
0
,
'ello'
,
-
5
)
test
(
'startswith'
,
'hello'
,
1
,
'ello'
,
-
4
)
test
(
'startswith'
,
'hello'
,
0
,
'o'
,
-
2
)
test
(
'startswith'
,
'hello'
,
1
,
'o'
,
-
1
)
test
(
'startswith'
,
'hello'
,
1
,
''
,
-
3
,
-
3
)
test
(
'startswith'
,
'hello'
,
0
,
'lo'
,
-
9
)
test
(
'endswith'
,
'hello'
,
1
,
'lo'
)
test
(
'endswith'
,
'hello'
,
0
,
'he'
)
test
(
'endswith'
,
'hello'
,
1
,
''
)
...
...
@@ -238,6 +250,21 @@ def run_method_tests(test):
test
(
'endswith'
,
'ab'
,
0
,
'ab'
,
0
,
1
)
test
(
'endswith'
,
'ab'
,
0
,
'ab'
,
0
,
0
)
# test negative indices in endswith
test
(
'endswith'
,
'hello'
,
1
,
'lo'
,
-
2
)
test
(
'endswith'
,
'hello'
,
0
,
'he'
,
-
2
)
test
(
'endswith'
,
'hello'
,
1
,
''
,
-
3
,
-
3
)
test
(
'endswith'
,
'hello'
,
0
,
'hello world'
,
-
10
,
-
2
)
test
(
'endswith'
,
'helloworld'
,
0
,
'worl'
,
-
6
)
test
(
'endswith'
,
'helloworld'
,
1
,
'worl'
,
-
5
,
-
1
)
test
(
'endswith'
,
'helloworld'
,
1
,
'worl'
,
-
5
,
9
)
test
(
'endswith'
,
'helloworld'
,
1
,
'world'
,
-
7
,
12
)
test
(
'endswith'
,
'helloworld'
,
1
,
'lowo'
,
-
99
,
-
3
)
test
(
'endswith'
,
'helloworld'
,
1
,
'lowo'
,
-
8
,
-
3
)
test
(
'endswith'
,
'helloworld'
,
1
,
'lowo'
,
-
7
,
-
3
)
test
(
'endswith'
,
'helloworld'
,
0
,
'lowo'
,
3
,
-
4
)
test
(
'endswith'
,
'helloworld'
,
0
,
'lowo'
,
-
8
,
-
2
)
test
(
'zfill'
,
'123'
,
'123'
,
2
)
test
(
'zfill'
,
'123'
,
'123'
,
3
)
test
(
'zfill'
,
'123'
,
'0123'
,
4
)
...
...
Misc/NEWS
Dosyayı görüntüle @
1f68fc7f
...
...
@@ -6,6 +6,9 @@ Type/class unification and new-style classes
Core and builtins
- Fixed string.startswith and string.endswith builtin methods
so they accept negative indices. [SF bug 493951]
- Fixed a bug with a continue inside a try block and a yield in the
finally clause. [SF bug 567538]
...
...
Objects/stringobject.c
Dosyayı görüntüle @
1f68fc7f
...
...
@@ -1310,6 +1310,21 @@ _PyString_Join(PyObject *sep, PyObject *x)
return
string_join
((
PyStringObject
*
)
sep
,
x
);
}
static
void
string_adjust_indices
(
int
*
start
,
int
*
end
,
int
len
)
{
if
(
*
end
>
len
)
*
end
=
len
;
else
if
(
*
end
<
0
)
*
end
+=
len
;
if
(
*
end
<
0
)
*
end
=
0
;
if
(
*
start
<
0
)
*
start
+=
len
;
if
(
*
start
<
0
)
*
start
=
0
;
}
static
long
string_find_internal
(
PyStringObject
*
self
,
PyObject
*
args
,
int
dir
)
{
...
...
@@ -1332,16 +1347,7 @@ string_find_internal(PyStringObject *self, PyObject *args, int dir)
else
if
(
PyObject_AsCharBuffer
(
subobj
,
&
sub
,
&
n
))
return
-
2
;
if
(
last
>
len
)
last
=
len
;
if
(
last
<
0
)
last
+=
len
;
if
(
last
<
0
)
last
=
0
;
if
(
i
<
0
)
i
+=
len
;
if
(
i
<
0
)
i
=
0
;
string_adjust_indices
(
&
i
,
&
last
,
len
);
if
(
dir
>
0
)
{
if
(
n
==
0
&&
i
<=
last
)
...
...
@@ -1763,16 +1769,8 @@ string_count(PyStringObject *self, PyObject *args)
else
if
(
PyObject_AsCharBuffer
(
subobj
,
&
sub
,
&
n
))
return
NULL
;
if
(
last
>
len
)
last
=
len
;
if
(
last
<
0
)
last
+=
len
;
if
(
last
<
0
)
last
=
0
;
if
(
i
<
0
)
i
+=
len
;
if
(
i
<
0
)
i
=
0
;
string_adjust_indices
(
&
i
,
&
last
,
len
);
m
=
last
+
1
-
n
;
if
(
n
==
0
)
return
PyInt_FromLong
((
long
)
(
m
-
i
));
...
...
@@ -2169,7 +2167,7 @@ string_startswith(PyStringObject *self, PyObject *args)
const
char
*
prefix
;
int
plen
;
int
start
=
0
;
int
end
=
-
1
;
int
end
=
INT_MAX
;
PyObject
*
subobj
;
if
(
!
PyArg_ParseTuple
(
args
,
"O|O&O&:startswith"
,
&
subobj
,
...
...
@@ -2193,23 +2191,15 @@ string_startswith(PyStringObject *self, PyObject *args)
else
if
(
PyObject_AsCharBuffer
(
subobj
,
&
prefix
,
&
plen
))
return
NULL
;
/* adopt Java semantics for index out of range. it is legal for
* offset to be == plen, but this only returns true if prefix is
* the empty string.
*/
if
(
start
<
0
||
start
+
plen
>
len
)
string_adjust_indices
(
&
start
,
&
end
,
len
);
if
(
start
+
plen
>
len
)
return
PyBool_FromLong
(
0
);
if
(
!
memcmp
(
str
+
start
,
prefix
,
plen
))
{
/* did the match end after the specified end? */
if
(
end
<
0
)
return
PyBool_FromLong
(
1
);
else
if
(
end
-
start
<
plen
)
return
PyBool_FromLong
(
0
);
else
return
PyBool_FromLong
(
1
);
}
else
return
PyBool_FromLong
(
0
);
if
(
end
-
start
>=
plen
)
return
PyBool_FromLong
(
!
memcmp
(
str
+
start
,
prefix
,
plen
));
else
return
PyBool_FromLong
(
0
);
}
...
...
@@ -2228,8 +2218,7 @@ string_endswith(PyStringObject *self, PyObject *args)
const
char
*
suffix
;
int
slen
;
int
start
=
0
;
int
end
=
-
1
;
int
lower
,
upper
;
int
end
=
INT_MAX
;
PyObject
*
subobj
;
if
(
!
PyArg_ParseTuple
(
args
,
"O|O&O&:endswith"
,
&
subobj
,
...
...
@@ -2253,15 +2242,17 @@ string_endswith(PyStringObject *self, PyObject *args)
else
if
(
PyObject_AsCharBuffer
(
subobj
,
&
suffix
,
&
slen
))
return
NULL
;
if
(
start
<
0
||
start
>
len
||
slen
>
len
)
return
PyBool_FromLong
(
0
);
string_adjust_indices
(
&
start
,
&
end
,
len
);
upper
=
(
end
>=
0
&&
end
<=
len
)
?
end
:
len
;
lower
=
(
upper
-
slen
)
>
start
?
(
upper
-
slen
)
:
start
;
if
(
end
-
start
<
slen
||
start
>
len
)
return
PyBool_FromLong
(
0
)
;
if
(
upper
-
lower
>=
slen
&&
!
memcmp
(
str
+
lower
,
suffix
,
slen
))
return
PyBool_FromLong
(
1
);
else
return
PyBool_FromLong
(
0
);
if
(
end
-
slen
>
start
)
start
=
end
-
slen
;
if
(
end
-
start
>=
slen
)
return
PyBool_FromLong
(
!
memcmp
(
str
+
start
,
suffix
,
slen
));
else
return
PyBool_FromLong
(
0
);
}
...
...
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