Coder Social home page Coder Social logo

Comments (5)

Emasoft avatar Emasoft commented on June 15, 2024 1

@MeilCli My suggestion is to simplify things:

  1. Convert internally all characters of the string to a separate Span object (with all attributes cloned from the original Span). One character for each Span.
  2. Use the array of characters ( List< Span > ) to do all string operations (like a char array, only having Spans instead of chars)
  3. Convert back the List< Span > to a FormattedText string

To help rebuilding the original FormattedText string Span structure, you can create a subclass of Span:

public class OneCharSpan: Span{
public int span_number;
}

And then using it in place of the Span class to build your List:

List< OneCharSpan > my_text_chars = new List< OneCharSpan > ();

foreach (var spanItem in FormattedText.Spans){
foreach(var charItem in spanItem.ToCharArray()){
var one_char_span = new OneCharSpan(){ ...copy char and attributes here... };
my_text_chars.Add(one_char_span);
}
}

In that way when you rebuild the spans in step 3, you can merge all OneCharSpan objects with the same span_number in a single Span object, and use the span_number to order the resulting spans when recreating the FormattedText string.

With this method you will make all operations on List< OneCharSpan > , and the user will not have to worry about Spans. The user shall use the FormattedText exactly the same way as he uses a normal String.

In the future you can optimize this easily. Just replace the OneCharSpan class with a new lighter class referencing the original spans formatting instead of duplicating it in each character. This shall occupy less memory and will be faster. But start with the basic algorithm above first.

from crossformattedtext.

MeilCli avatar MeilCli commented on June 15, 2024 1

@Emasoft Thank you for your suggestion. Please give me time to try it in that direction.

from crossformattedtext.

MeilCli avatar MeilCli commented on June 15, 2024

Thanks nice feature request!!

First, I am not good at English.
I tried reading English as much as possible but it looks like the following:

I think add method to FormattedString class

  • String like method:
    • Contains, call Contains of concatted all span text
    • EndsWith, call EndsWith of last span text
    • IndexOf, call IndexOf of concatted all span text
    • Insert, clone span data where inserted position ※Notes
    • LastIndexOf, call LastIndexOf of concatted all span text
    • Remove, span that become empty text will removed from FormattedString
    • Replace, call Replace of each span text ※Notes
    • Split, I do not know how to implement it ※Notes
    • StartsWith, call StartsWith of first span text
    • Substring, clone span data
    • ToLower and ToUpper, call its method of each span
    • Trim, call TrimStart of first span text and call TrimEnd of end span text
    • TrimStart and TrimEnd, call its method of first or last span
  • String like method using span
    • Maybe the same as above
  • Parse method
    • ToString(override), to html like string
    • Parse(static), from html like string

In the above method call, FormattedString must be immutable.
So I'd like to prepare the FormmatedStringBuilder class and probably do that.

※Notes
Behavior of the joint of the span is difficult.

example:

Span1 Span2 Span3
some loop occurred 1 200 times
  • Insert text in behind of 'p' in Span1. which insert Span1 last position or Span2 first position?
  • Replace to 200 from 1200 in Span2-Span3, How to insert?

Please give me a good idea.
Thank you!

from crossformattedtext.

MeilCli avatar MeilCli commented on June 15, 2024

work finished.

This feature is available in version 2.0.0

from crossformattedtext.

Emasoft avatar Emasoft commented on June 15, 2024

Awesome news!! Thank you!

from crossformattedtext.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.