+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 15 of 16

Thread: LibString

  1. #1
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default LibString

    Hey all.

    I had a bunch of string functions lying around which I was using in other projects so I thought I might as well squeeze them into a Rift library. So I polished the code a bit and added some tests, just in case. The thing can be found at Curseforge and RiftUI.

    Here is the summary of new functions:
    • string.concat concatenates a list of strings.
    • string.formatn enhances the default string.format with index-specifiers like "%1s %10i" for accessing arguments out-of-order. This is especially useful for localization.
    • string.insert inserts a string into another at a specified index.
    • string.join concatenates a list of strings by inserting a separator string between each.
    • string.prefix checks whether a string is a prefix of another (i.e. starts with).
    • string.split splits a string at given delimeters and returns the respective substrings either as list or table.
    • string.suffix checks whether a string is a suffix of another (i.e. ends with).
    • string.tostring accepts a list of arguments and returns a list of strings (Lua's ##tostring## accepts only one argument).
    • string.ltrim, string.rtrim and string.trim remove characters at the beginning, end or both ends of a string, respectively.
    More detailed documentation can be found in the wiki pages.

    If you have ideas for new methods or any other comments/additions just shoot.
    Last edited by Imhothar; 03-16-2012 at 07:38 PM.

  2. #2
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    How are the concat/join functions different from table.concat(table) or table.concat(table, seperator)?

    ... not to say I knew about that before this last week or so.

    BTW, if I were doing .split, I'd just have it return an array, because the user can always unpack() it.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  3. #3
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Quote Originally Posted by the_real_seebs View Post
    How are the concat/join functions different from table.concat(table) or table.concat(table, seperator)?

    ... not to say I knew about that before this last week or so.
    The only difference is that concat and join can be called with a list of arguments
    Code:
    string.join(":", a, b, c, d)
    string.join(":", ...)
    They are just neat wrappers around table.concat, nothing more.

    Quote Originally Posted by the_real_seebs View Post
    BTW, if I were doing .split, I'd just have it return an array, because the user can always unpack() it.
    Calling it with true as third argument does exaclty that
    Could swap the behaviour, but that depends on what gets used more often.

  4. #4
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Ok, you convinced me. string.split() now returns a table by default and unpack()s it if the third argument evaluates to true.

    It's up as 1.0beta5

  5. #5
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    I have a string tokenizer buried in LibGetopt. I will eventually revamp it to allow controlling whether it recognizes any or all of single quotes, double quotes, and backslashes; right now it just does double quotes and backslashes.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  6. #6
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Quote Originally Posted by the_real_seebs View Post
    I have a string tokenizer buried in LibGetopt. I will eventually revamp it to allow controlling whether it recognizes any or all of single quotes, double quotes, and backslashes; right now it just does double quotes and backslashes.
    string.split can do that as it uses patterns for separating.
    Code:
    ("a,b.c:d\"e"):split("[,:\.\"]")
    returns { "a", "b", "c", "d", "e" }
    
    ("asepbsepcsep"):split("sep")
    returns { "a", "b", "c", "" }

  7. #7
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    I'm thinking of making a separate string.gsplit() (or some other name) for the pattern matching in order to increase performance for people who use string.split() without patterns.

  8. #8
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    Actually, I don't think split can do it in the general case, because the interactions of quotes and backslashes are non-obvious.

    The purpose of the tokenizer is to recognize that:

    Code:
    a = b <= 3 tokens, <a> <=> <b>
    "a = b" <= 1 token, <a = b>
    a = "b = c" <= 3 tokens, <a> <=> <b = c>
    x = \"hello, world!\" <= 3 tokens, <x> <=> <"hello, world!">
    Currently it just ignores single quotes, but is otherwise basically like Unix shell command line semantics -- which is intentional, that's why LibGetOpt has it.

    So you can have

    Code:
    /mycommand -a 3 -b "this is a test" -c more words
    come out to:

    Code:
    {
    a = 3,
    b = 'this is a test',
    leftover_args = { 'more', 'words' },
    leftover = 'more words'
    }
    I use this in my addons because it's a heck of a lot easier to write "/goofball -g" and just check for args.g in my slashcommand function than it would be to actually do the string parsing.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  9. #9
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Well of course not. It's just meant for simple tokenizing, not grammar parsing.

  10. #10
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    Yeah. I just found a quote-capable tokenizer super useful, because it means that I can put ANYTHING in words, not just things-which-aren't-spaces. I should totally improve it, though.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  11. #11
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Your comments so far made me think about string.split a bit and I changed it a bit.
    First, it now always returns a table, you were right about people being able to just unpack() it.
    Seconds, the third argument now controls whether the splitting is performed on using a search pattern or a plain string (which is the default and faster).

    Plus I added a new function inspired by Lua's gmatch:
    Code:
    for s, sep in string.gsplit(",a,b/c:d/", "[,/:]", true) do
        print(s, sep)
        print("----")
    end
    prints the lines
    Code:
    	,
    ----
    a	,
    ----
    b	/
    ----
    c	:
    ----
    d	/
    ----
    	
    ----
    Compared to string.split it has the advantage of not needing a temporary table and it returns the separator string which can be used for more advanced processing. The new stuff is available in 1.0beta6
    Last edited by Imhothar; 03-06-2012 at 04:03 PM.

  12. #12
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    Quote Originally Posted by the_real_seebs View Post
    So you can have

    Code:
    /mycommand -a 3 -b "this is a test" -c more words
    come out to:

    Code:
    {
    a = 3,
    b = 'this is a test',
    leftover_args = { 'more', 'words' },
    leftover = 'more words'
    }
    Shouldn't it be
    Code:
    {
    a = 3,
    b = 'this is a test',
    c = 'more',
    leftover_args = { 'words' },
    leftover = 'words'
    }
    At least that's what I would expect it to be.

  13. #13
    RIFT Community Ambassador the_real_seebs's Avatar
    Join Date
    Jan 2011
    Posts
    16,859

    Default

    argh. It should have been 'c = true'.

    Long story short: You can specify whether a letter is a boolean flag, a counter, or takes an argument.

    It's been really useful for me, anyway.
    You can play WoW in any MMO. You don't have to play WoW in RIFT. Oh, and no, RIFT is not a WoW clone. Not having fun any more? Learn to play, noob! I don't speak for Riftui, but I moderate stuff there. Just came back? Welcome back! Here's what's changed. (Updated for 2.5!)

  14. #14
    Champion Lorandii's Avatar
    Join Date
    Jun 2011
    Posts
    516

    Default Re: Curseforge & Curse

    LibString is now approved.

  15. #15
    Plane Walker Imhothar's Avatar
    Join Date
    Feb 2012
    Posts
    439

    Default

    As I didn't get any bug reports so far the library has left beta status.

    Enjoy.

    Edit: It's also available on RiftUI.
    Last edited by Imhothar; 03-16-2012 at 07:37 PM.

+ Reply to Thread
Page 1 of 2 1 2 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts