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
101923bb
Kaydet (Commit)
101923bb
authored
Nis 02, 1997
tarafından
Guido van Rossum
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Added replace() implementation by Perry Stoll (debugged and reformatted by me).
üst
f643e3ff
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
167 additions
and
1 deletion
+167
-1
stropmodule.c
Modules/stropmodule.c
+167
-1
No files found.
Modules/stropmodule.c
Dosyayı görüntüle @
101923bb
...
@@ -798,6 +798,171 @@ strop_translate(self, args)
...
@@ -798,6 +798,171 @@ strop_translate(self, args)
}
}
/* What follows is used for implementing replace(). Perry Stoll. */
/*
mymemfind
strstr replacement for arbitrary blocks of memory.
Locates the first occurance in the memory pointed to by MEM of the
contents of memory pointed to by PAT. Returns the index into MEM if
found, or -1 if not found. If len of PAT is greater than length of
MEM, the function returns -1.
*/
static
int
mymemfind
(
mem
,
len
,
pat
,
pat_len
)
char
*
mem
;
int
len
;
char
*
pat
;
int
pat_len
;
{
register
int
ii
;
/* pattern can not occur in the last pat_len-1 chars */
len
-=
pat_len
;
for
(
ii
=
0
;
ii
<=
len
;
ii
++
)
{
if
(
mem
[
ii
]
==
pat
[
0
]
&&
(
pat_len
==
1
||
memcmp
(
&
mem
[
ii
+
1
],
&
pat
[
1
],
pat_len
-
1
)
==
0
))
{
return
ii
;
}
}
return
-
1
;
}
/*
mymemcnt
Return the number of distinct times PAT is found in MEM.
meaning mem=1111 and pat==11 returns 2.
mem=11111 and pat==11 also return 2.
*/
static
int
mymemcnt
(
mem
,
len
,
pat
,
pat_len
)
char
*
mem
;
int
len
;
char
*
pat
;
int
pat_len
;
{
register
int
offset
=
0
;
int
nfound
=
0
;
while
(
len
>=
0
)
{
offset
=
mymemfind
(
mem
,
len
,
pat
,
pat_len
);
if
(
offset
==
-
1
)
break
;
mem
+=
offset
+
pat_len
;
len
-=
offset
+
pat_len
;
nfound
++
;
}
return
nfound
;
}
/*
mymemreplace
Return a string in which all occurences of PAT in memory STR are
replaced with SUB.
If length of PAT is less than length of STR or there are no occurences
of PAT in STR, then the original string is returned. Otherwise, a new
string is allocated here and returned.
on return, out_len is:
the length of output string, or
-1 if the input string is returned, or
unchanged if an error occurs (no memory).
return value is:
the new string allocated locally, or
NULL if an error occurred.
*/
static
char
*
mymemreplace
(
str
,
len
,
pat
,
pat_len
,
sub
,
sub_len
,
out_len
)
char
*
str
;
int
len
;
/* input string */
char
*
pat
;
int
pat_len
;
/* pattern string to find */
char
*
sub
;
int
sub_len
;
/* substitution string */
int
*
out_len
;
{
char
*
out_s
;
char
*
new_s
;
int
nfound
,
offset
,
new_len
;
if
(
len
==
0
||
pat_len
>
len
)
goto
return_same
;
/* find length of output string */
nfound
=
mymemcnt
(
str
,
len
,
pat
,
pat_len
);
if
(
nfound
==
0
)
goto
return_same
;
new_len
=
len
+
nfound
*
(
sub_len
-
pat_len
);
new_s
=
(
char
*
)
malloc
(
new_len
);
if
(
new_s
==
NULL
)
return
NULL
;
*
out_len
=
new_len
;
out_s
=
new_s
;
while
(
len
>
0
)
{
/* find index of next instance of pattern */
offset
=
mymemfind
(
str
,
len
,
pat
,
pat_len
);
/* if not found, break out of loop */
if
(
offset
==
-
1
)
break
;
/* copy non matching part of input string */
memcpy
(
new_s
,
str
,
offset
);
/* copy part of str before pat */
str
+=
offset
+
pat_len
;
/* move str past pattern */
len
-=
offset
+
pat_len
;
/* reduce length of str remaining */
/* copy substitute into the output string */
new_s
+=
offset
;
/* move new_s to dest for sub string */
memcpy
(
new_s
,
sub
,
sub_len
);
/* copy substring into new_s */
new_s
+=
sub_len
;
/* offset new_s past sub string */
}
/* copy any remaining values into output string */
if
(
len
>
0
)
memcpy
(
new_s
,
str
,
len
);
return
out_s
;
return_same
:
*
out_len
=
-
1
;
return
str
;
}
static
PyObject
*
strop_replace
(
self
,
args
)
PyObject
*
self
;
/* Not used */
PyObject
*
args
;
{
char
*
str
,
*
pat
,
*
sub
,
*
new_s
;
int
len
,
pat_len
,
sub_len
,
out_len
;
PyObject
*
new
;
if
(
!
PyArg_ParseTuple
(
args
,
"s#s#s#"
,
&
str
,
&
len
,
&
pat
,
&
pat_len
,
&
sub
,
&
sub_len
))
return
NULL
;
new_s
=
mymemreplace
(
str
,
len
,
pat
,
pat_len
,
sub
,
sub_len
,
&
out_len
);
if
(
new_s
==
NULL
)
{
PyErr_NoMemory
();
return
NULL
;
}
if
(
out_len
==
-
1
)
{
/* we're returning another reference to the input string */
new
=
PyTuple_GetItem
(
args
,
0
);
Py_XINCREF
(
new
);
}
else
{
new
=
PyString_FromStringAndSize
(
new_s
,
out_len
);
free
(
new_s
);
}
return
new
;
}
/* List of functions defined in the module */
/* List of functions defined in the module */
static
PyMethodDef
static
PyMethodDef
...
@@ -811,13 +976,14 @@ strop_methods[] = {
...
@@ -811,13 +976,14 @@ strop_methods[] = {
{
"joinfields"
,
strop_joinfields
,
1
},
{
"joinfields"
,
strop_joinfields
,
1
},
{
"lstrip"
,
strop_lstrip
},
{
"lstrip"
,
strop_lstrip
},
{
"lower"
,
strop_lower
},
{
"lower"
,
strop_lower
},
{
"maketrans"
,
strop_maketrans
,
1
},
{
"replace"
,
strop_replace
,
1
},
{
"rfind"
,
strop_rfind
,
1
},
{
"rfind"
,
strop_rfind
,
1
},
{
"rstrip"
,
strop_rstrip
},
{
"rstrip"
,
strop_rstrip
},
{
"split"
,
strop_splitfields
,
1
},
{
"split"
,
strop_splitfields
,
1
},
{
"splitfields"
,
strop_splitfields
,
1
},
{
"splitfields"
,
strop_splitfields
,
1
},
{
"strip"
,
strop_strip
},
{
"strip"
,
strop_strip
},
{
"swapcase"
,
strop_swapcase
},
{
"swapcase"
,
strop_swapcase
},
{
"maketrans"
,
strop_maketrans
,
1
},
{
"translate"
,
strop_translate
,
1
},
{
"translate"
,
strop_translate
,
1
},
{
"upper"
,
strop_upper
},
{
"upper"
,
strop_upper
},
{
NULL
,
NULL
}
/* sentinel */
{
NULL
,
NULL
}
/* sentinel */
...
...
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