Skip to content

Proposal: String Case Manipulation #874

Closed

Description

@mlucy

People should be able to convert strings to lower or upper case. Ruby and CL both use upcase and downcase for this, which I really like as names:

> r("Ab").upcase.run
"AB"
> r("Ab").downcase.run
"ab"

Activity

  1. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    @neumino I think that just because lowercase happens to be a frequently searched word doesn't necessarily make it a good choice it's obviously going to have a lot more people searching for it since it's an actual english word. toUpper and toLower are the ones that sound the most natural to me.

    Also science:
    http://www.google.com/trends/explore#q=upcase%2C%20toUpper&cmpt=q

  2. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    I'm cool with toUpper/toLower and upcase/downcase, but not toUpperCase/toUppercase/toLowerCase/toLowercase/upperCase/lowerCase. All of the latter examples seem really bad to me.

    I wouldn't worry too much about people searching through the docs and not finding what they want because we can solve it pretty easily by just putting keywords into the description.

  3. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    What about iDesireThatWhichIsMyFirstArgumentToBeUpperCasePlease?

    Edit: manners.

  4. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    What about iDesireThatWhichIsMyFirstArgumentToBeUpperCasePlease?

    I'm cool with that, but only as long as we change every other command we have to end with Please. It's actually really nice:

    r.tablePlease('foo')
    r.tablePlease('foo').getPlease('bar')
    r.tablePlease('foo').getPlease('bar').updatePlease({ 'politeness': 'yes' })
    

    There's a lot to be said for a codebase with good manners.

  5. coffeemug commented on Jun 11, 2013

    @coffeemug
    Contributor

    Or alternatively, to save typing, we could just change run to runPlease:

    r.table('foo').runPlease()
    r.table('foo').get('bar').runPlease()
    r.table('foo').get('bar').update({ 'politeness': 'yes' }).runPlease()
    

    Not much typing, and very polite. It's a win-win.

  6. jdoliner commented on Jun 11, 2013

    @jdoliner
    Contributor

    I assume @neumino and @AtnNn are going to start the classic runPlease vs. runSiVousPlait debate any second now.

  7. neumino commented on Jun 11, 2013

    @neumino
    Member

    I would be fine with both if runPlease returns some random results from time to time (but not runSilVousPlait)

  8. mlucy commented on Jun 21, 2013

    @mlucy
    MemberAuthor

    Alright, here's the summary:

    • upcase -- mlucy, Slava, ruby
    • upper -- python
    • to_upper -- Joe, Slava
    • to_upper_case -- JS, Michel

    Since the discussion period is over, and there's no real consensus, I arbitrarily pick upcase, since two people liked it and it's the name used by one of our client languages. The corresponding command will be downcase.

  9. coffeemug commented on Aug 6, 2013

    @coffeemug
    Contributor

    For those watching this issue, it will almost certainly happen before RethinkDB 2.0, but not before 1.9 and possibly 1.10/1.9.1 is out. There is a bit of accumulated technical debt we have to pay back, and we're trying to push even small enhancements back until that done.

    Sorry if this causes you pain, if it's really important to you, please let us know and we'll see if we can reprioritize.

  10. ghost assigned on Nov 22, 2013
  11. coffeemug commented on Nov 25, 2013

    @coffeemug
    Contributor

    Restarting this discussion, after sitting on this for a few months, I'm cool with upcase/downcase (and actually prefer it). We can use keywords lower/upper in the docs to make sure people find these commands, so I don't think search is actually a problem.

  12. mlucy commented on Nov 26, 2013

    @mlucy
    MemberAuthor

    This is tagged as API_settled from last time we discussed it, so I concur. upcase/downcase it is.

  13. 5 remaining items

  14. danielmewes commented on Dec 4, 2013

    @danielmewes
    Member

    I strongly think that we should implement unicode-aware case-manipulation.
    That is what people expect. Our data format is JSON. JSON uses Unicode.

    There is support for it in Boost.
    http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/index.html

  15. mlucy commented on Dec 4, 2013

    @mlucy
    MemberAuthor

    JSON uses UTF-8 because it's a format for storing and retrieving text. If that's all you're doing, there's no reason not to have Unicode support. We also support storing and retrieving Unicode text.

    Committing to manipulating Unicode text correctly is entirely different.

    There are three main reasons I don't want to do this:

    • Implementing upcase and downcasecorrectly is probably doable with a bit of work, but it will create the expectation that RethinkDB can do unicode-aware string manipulation, and I don't think that we want to promise that.
    • Unicode transformations are, in general, insane.
      • I don't believe that it's possible to upper-case a Unicode string without reference to a locale. The boost library you linked to takes a locale for its to_upper function, and produces different results in different locales. I don't think we want to make the result of ReQL operations depend on what locale you're in, for obvious reasons, and if we settle on an agnostic standard like the C locale, people will get unexpected results from upper-casing strings.
      • Doing "correct" Unicode transformations can cause extremely non-obvious things to happen. For example, in some locales, upper-casing a Unicode string with ICU can change its length. Not its visual length, but the actual number of code points.
      • The boost library you link to can either use ICU as the backend, or use a smaller non-ICU boost backend. (Note that case-manipulation on Unicode strings produces different results depending on which backend you're using, since the ICU backend will sometimes convert one code point to many or vice-versa while changing case and the pure-boost backend will not.) I don't think we want to package ICU with RethinkDB, but I also don't think we want to give people the custom non-ICU boost backend, which cursory googling suggests isn't very smart.
    • Most other languages don't do this. Ruby, for example, doesn't touch Unicode characters:
    >> a = "gr\303\274\303\237EN\n"; puts a; puts a.downcase; puts a.upcase
    grüßEN
    grüßen
    GRüßEN
  16. mlucy commented on Dec 4, 2013

    @mlucy
    MemberAuthor

    Actually, on the topic of locale-specific behaviors, if we have deterministic ReQL terms which behave differently in different locales (which I think we'd have to for Unicode upcase/downcase) then we'd have to ensure that all servers in a cluster reside in the same locale, since otherwise you could get data divergence between nodes if you do a locale-dependent atomic update.

  17. AtnNn commented on Dec 4, 2013

    @AtnNn
    Member

    We already link to ICU transitively through v8 on certain platforms.

    Most other languages were designed before Unicode became ubiquitous. See Python 3 and Go for better examples of Unicode support by default.

  18. mlucy commented on Dec 4, 2013

    @mlucy
    MemberAuthor

    Oh, that's cool then. (Note that we won't be able to link against different versions of ICU on different platforms unless we are 100% sure that they always produce the same output given the same input (see my previous comment about data divergence). I'm not sure how much of a wrench that throws into the packaging.)

    Python 3.2.3 on Newton appears not to upper-case or lower-case Unicode characters either:

    >>> a = 'gr\303\274\303\237EN\n'.encode('utf-8')
    >>> a
    b'gr\xc3\x83\xc2\xbc\xc3\x83\xc2\x9fEN\n'
    >>> a.upper()
    b'GR\xc3\x83\xc2\xbc\xc3\x83\xc2\x9fEN\n'
    >>> a.lower()
    b'gr\xc3\x83\xc2\xbc\xc3\x83\xc2\x9fen\n'

    I don't know enough Go to test easily, but I would give it even odds that it doesn't touch Unicode characters when upper/lower-casing either.

    The other objections (setting expectations, locale-dependency, and unintuitive behavior) still stand.

  19. danielmewes commented on Dec 4, 2013

    @danielmewes
    Member

    Well, the question is how much we want to commit to doing string manipulation at all.
    If we want to commit to that, we should do it properly and support Unicode. Otherwise those operations would be completely crippled for most applications I can think of (everything that deals with text on the web is usually expected to work with Unicode for example).

    upcase and downcase are basic enough that they can have some limited use cases, even when crippled like that (most of which I guess do not involve user-supplied data). But even comparing user-provided text in a case-insensitive way wouldn't work properly, except for the 5% of people in the world who use English as their first language and the few more who use primarily a language which uses the same alphabet. [1]

    I guess I would be ok with having upcase and downcase not properly support Unicode, but we should very clearly explain that fact in the documentation.

    Having a change of encoding length doesn't seem to be a problem in our case, is it? I don't think we are going to use these functions for in-place manipulation, are we?

    Being dependent on a locale would indeed be annoying. On the other hand we already support locales for our time functions, so it wouldn't be such a huge step. I'm also not sure in how far using a default locale would make these functions less useful. As far as I know most web services run with a fixed locale for their string processing, and they don't seem to run into problems due to it (but I might just not be aware of the cases where they do).

    [1] I know this is a bit biased. When dealing with computers, many more people use primarily English. However there's still a significant number of people for whom missing Unicode support would be an issue.

  20. danielmewes commented on Dec 4, 2013

    @danielmewes
    Member

    Something which might be interesting about locale-dependency:
    In PHP, there are two functions. strtolower http://php.net/manual/en/function.strtolower.php
    and mb_strtolower http://www.php.net/manual/en/function.mb-strtolower.php .

    The former depends on the locale, the latter does not:

    By contrast to strtolower(), 'alphabetic' is determined by the Unicode character properties. Thus the behaviour of this function is not affected by locale settings and it can convert any characters that have 'alphabetic' property, such as A-umlaut (Ä).

    I don't know enough about Unicode to fully understand the implications here, but there seems to be a way of doing this in a locale-independent way.

    Edit: Actually there is a comment on that page which gives an example of why locale matters:

    Turkish is a good example of this. In Turkish, the letter I/i has a dotted-upper-case form (İ) and a dotless-lower-case form (ı).

    https://en.wikipedia.org/wiki/Dotted_and_dotless_I

    This means that you cannot correctly convert between upper-case and lower-case without also knowing the locale of the data.

    Since the function does not let you specify a locale, you should only use this function for text written in languages that follow the same orthography as English.

    I guess this is kind of tricky.

  21. mlucy commented on Dec 4, 2013

    @mlucy
    MemberAuthor

    I definitely agree that we should try to keep RethinkDB's string manipulation to a minimum. If we ever decide to push further into the realm of string processing, we may want to take a second look at supporting Unicode text-manipulation.

    I think "crippled" is a little strong though. English is used by way more than 5% of the web (Wikipedia places it at 55% (http://en.wikipedia.org/wiki/Languages_used_on_the_Internet), and it doesn't seem to be going down very quickly (http://w3techs.com/technologies/history_overview/content_language/ms/y)). For the particular subset of the web that's likely to be RethinkDB customers, I think 55% is probably a large under-estimate.

    This isn't to say that other languages aren't important -- they definitely are -- but it's not like we're adding a feature that will only work for 5% of people using the product.

    Also, empirically, the fact that e.g. Ruby and Python don't seem to support Unicode case-manipulation out of the box doesn't seem to have hurt their adoption as web languages.


    I don't really understand the locale issue as well as I'd like. I've spent about 20 minutes googling around, and I can't seem to find a straight answer as to how en_US.UTF-8 and de_DE.UTF-8 are treated differently by different libraries.

    The boost library requires that a locale be passed to the to_upper function, and the documentation seems to suggest that it affects the output.

    Maybe the normalization is different in different locales? (Many characters, like letters with accents over them, can be produced by multiple different sequences of code points, and most Unicode processing libraries consider these different sequences of code points to be equivalent. To make life easier, the Unicode text is usually normalized to a single canonical representation prior to processing.)

  22. danielmewes commented on Dec 4, 2013

    @danielmewes
    Member

    So yeah I guess I would be ok with this limited toupper/tolower, but it still feels like a bad compromise.
    I think the behavior of PHP's mb_strtolower would be almost strictly preferable. It still has limitations, but it sets the bar a lot higher and isn't dependent on locale. If we could find a reasonable easy way of getting that behavior without huge additional libraries I think we should do that.

    Also note that 55% of the web content being English doesn't mean that 55% of web services can get away with supporting only English. Many web services have to deal with multiple languages, even though most of their content might be English. That aside, I did of course exaggerate with the 5% and the truth is probably somewhere in between.

  23. AtnNn commented on Dec 4, 2013

    @AtnNn
    Member

    Following all this discussion, I believe that i18n is out of the scope of this issue.

    @mlucy your python example uses bytestrings, not unicode strings. Go has the same behaviour as Python 3:

    >>> "gr\u00FC\u00DFEN"
    'grüßEN'
    >>> "grüßEN".upper()
    'GRÜSSEN'
    >>> "grüßEN".lower()
    'grüßen'
    
  24. mlucy commented on Dec 4, 2013

    @mlucy
    MemberAuthor

    @AtnNn -- What's i18n?
    (Also, it's pretty cool that Python 3 does that out of the box. I am legitimately surprised by that.)

  25. AtnNn commented on Dec 4, 2013

    @AtnNn
    Member

    i18n is internationalisation

  26. coffeemug commented on Dec 4, 2013

    @coffeemug
    Contributor

    I would strongly prefer to keep internationalization of these functions out of the scope of this issue. (We should go back to the problem, though, but not within the scope of 1.12)

  27. mlucy commented on Dec 6, 2013

    @mlucy
    MemberAuthor

    This is in next, sans internationalization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions