[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

New submission from Stéphane Henriot:

A few days ago, the following behavior surprised me.

>>> "".replace("", "prefix", 1)
''
>>> "".replace("", "prefix")
'prefix'

It seems to me this edge case isn't correctly documented/implemented. I tested 
with python 2.7, 3.4 and 3.5, I guess it applies for other versions as well.

Here are a few elements, for reference.

« string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by 
new. If the optional argument maxreplace is given, the first maxreplace 
occurrences are replaced. »

>>> "" in ""
True
>>> "".count("")
1
>>> "".find("")
0

https://hg.python.org/cpython/file/2.7/Objects/stringobject.c#l2750
https://hg.python.org/cpython/file/default/Objects/unicodeobject.c#l10479

Thanks,

Stéphane.

--
messages: 275140
nosy: Stéphane Henriot
priority: normal
severity: normal
status: open
title: Replace and empty strings
type: behavior

___
Python tracker 
<http://bugs.python.org/issue28029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

For what it's worth, here is the behavior in PyPy 2.2.1

>>>> "".replace("", "prefix", 1)
'prefix'
>>>> "".replace("", "prefix")
'prefix'

--

___
Python tracker 
<http://bugs.python.org/issue28029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

Thanks for your help.

However, I'm not sure I would agree, regarding the correct behavior.

I guess the main question is « What is an occurrence? ».

Are you not convinced that in, count and find indicate occurrences?
To my understanding, the empty string occurs basically everywhere in strings 
(including beginning and end):

>>> "blabla".replace("", "ε")
'εbεlεaεbεlεaε'

To give a bit more (useless?) context, I was implementing a GCD algorithm based 
on replacements by Knuth (very beginning of TAOCP) and it looks like the 
current cpython implementation gives 0 for gcd(1, 1) :(

--

___
Python tracker 
<http://bugs.python.org/issue28029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-08 Thread Stéphane Henriot

Stéphane Henriot added the comment:

I understand it might be a rather rare case.

Nevertheless, don't you think the inconsistency Serhiy pointed out makes it 
look like a bug needing a fix?

--

___
Python tracker 
<http://bugs.python.org/issue28029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue28029] Replace and empty strings

2016-09-09 Thread Stéphane Henriot

Changes by Stéphane Henriot :


--
nosy: +georg.brandl

___
Python tracker 
<http://bugs.python.org/issue28029>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com