DaveAKing / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Special case for SpannedStrings in Joiner #1687

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Was trying to concat some marked up text (with Android spans) and Joiner 
silently threw away the formatting.  I don't know if it's feasible, but it 
would be nice if Joiner could operate at the CharSeqeunce level.  At first 
guess, it should be possible as a StringBuilder is a CharSequence and it can 
append CharSequence's. 

Original issue reported on code.google.com by android....@gmail.com on 2 Mar 2014 at 6:14

GoogleCodeExporter commented 9 years ago
Can you give some detail about how this would differ from the current behavior? 
If your inputs are CharSequences, then Joiner should already be calling 
StringBuilder.append(CharSequence). Is that not happening, or do you want a 
different behavior?

Original comment by cpov...@google.com on 2 Mar 2014 at 2:33

GoogleCodeExporter commented 9 years ago
No, it's not working.

Neither:

    public static CharSequence concatLines(CharSequence... lines) {
        return Joiner.on(LINE_SEPARATOR).join(lines);
    }

My own method works:

Nor:

    public static CharSequence concatLines(CharSequence... lines)     {
        StringBuilder stringBuilder = new StringBuilder();
        Joiner.on(LINE_SEPARATOR).appendTo(stringBuilder, lines);
        return stringBuilder;
    }

My own method works:

    public static CharSequence concatLines(CharSequence... lines)   {
       CharSequence text = null;
        for (CharSequence line : lines)
              if (TextUtils.isEmpty(text))
                    text = line;
               else
                     text = TextUtils.concat(text, LINE_SEPARATOR, line);
        return text;
}

Original comment by android....@gmail.com on 2 Mar 2014 at 9:38

GoogleCodeExporter commented 9 years ago
Please explain the behavior you are seeing and the desired behavior.

Original comment by cpov...@google.com on 2 Mar 2014 at 9:40

GoogleCodeExporter commented 9 years ago
I am passing in some annotated CharSequences.  I have basically formatting 
Spans attached to them.  So they are Android SpannedString objects. 

http://developer.android.com/reference/android/text/SpannedString.html

But after I run them through a Joiner, the returned object has the formatting 
stripped out.  In the case of join(), it's expected, as the return is defined 
to be a String, so it's expected.  It would be nice if join() could return a 
CharSequence, or there'd be a new method joinToCharSequence.

But the 

StringBuilder stringBuilder = new StringBuilder();
Joiner.on(LINE_SEPARATOR).appendTo(stringBuilder, lines);
return stringBuilder;

pattern also strips out the formatting, so I suspect there is a .toString() 
some place in the Joiner code that does that.

Original comment by android....@gmail.com on 2 Mar 2014 at 9:48

GoogleCodeExporter commented 9 years ago
It sounds like you want a SpannedString as the output?

Original comment by cpov...@google.com on 2 Mar 2014 at 9:55

GoogleCodeExporter commented 9 years ago
This doesn't sound possible.  How could Joiner know how to append to something 
that doesn't at least implement Appendable?  How would you possibly implement 
this, OP?

Original comment by wasserman.louis on 2 Mar 2014 at 9:58

GoogleCodeExporter commented 9 years ago
I suppose so.  Like how android.text.TextUtils.concat() also maintains the 
spans.  But now, I am looking at the sources for TextUtils.concat() and it has 
special logic just for SpannedStrings, so that obviously not going to fly with 
Guava.  That's cool, now I understand what's going on.  You can close this 
issue as out of scope.

Original comment by android....@gmail.com on 2 Mar 2014 at 10:02

GoogleCodeExporter commented 9 years ago

Original comment by cpov...@google.com on 2 Mar 2014 at 10:04

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:09

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07