1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

Discussion string_pos_ext, string_lastpos

Discussion in 'GameMaker Studio 2 Community Tech Support' started by YellowAfterlife, Feb 6, 2017.

  1. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,435
    While we are in a time period where additions to standard function library can actually happen, I'd like to propose several string functions to bridge some gaps in compatibility with common APIs:

    string_pos_ext(substr, string, start)
    Much akin to regular string_pos, but with a specifiable offset to start search from.
    Mirrors the behaviour of optional second parameter in indexOf in common APIs.
    Other option would be to add an optional parameter to string_pos, but I think having a separate function would create less confusion (considering that Pascal-style argument order in string_pos already makes some).
    Current workarounds:
    • string_pos(substr, string_delete(string, 1, start - 1)) + (start - 1)
      Easy to implement, but not viable on larger strings due to allocating one string per search (since GM does not optimize substrings like JS does).
    • Custom search via a for-loop with string_char_at\string_copy comparisons.
      Not viable on large strings due to use of UTF-8 as format for string storage (meaning that string_char_at(_, N) is actually O(N) instead of O(1)).
    • Custom search via a "fast" buffer.
      Somewhat viable on large strings, but only if the algorithm is complex enough to justify longer setup time (string->fixed buffer->fast buffer).
    string_lastpos(substr, string)
    Finds the position of last occurrence of substr in string.
    Mirrors the behaviour of lastIndexOf in common APIs.
    Current workarounds:
    • Series of string_pos + string_delete calls to find the last occurrence.
      As mentioned above, not a good solution, except here the effect is even worse due to doing this a number of times in a row.
    • Custom search via a "fast" buffer.
      Same as above; Using string-based backwards search usually isn't viable even on average-sized strings.
    string_lastpos_ext(substr, string, end)
    Finds the position of last occurrence of substr in string, only searching until `end` position.
    Mirrors the behaviour of optional parameter version of lastIndexOf in common APIs.
    Current workarounds:
    • Same as for string_lastpos, except with slightly more overhead.
    Purpose:
    While all of these functions are trivial to implement (indexOf\lastIndexOf on JS, offset search for string_pos_ext on native, string::rfind for string_lastpos on native), they are priceless for parsers and other algorithms involving searching for consequent patterns in strings.
     
  2. rwkay

    rwkay YoYo Games Staff YYG Staff

    Joined:
    Apr 12, 2016
    Posts:
    1,049
    Please file as suggestions in Help -> Report a Bug

    Russell
     
  3. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,435
    Submitted.
     
  4. JeffJ

    JeffJ Member

    Joined:
    Jun 20, 2016
    Posts:
    342
    Was this suggestion ignored? The string function library is still severely lacking in GMS2. Would love to have, at the very least, these additions out of the box.

    EDIT
    This is a really good library
    https://marketplace.yoyogames.com/assets/3159/tmc-string

    Which I've been using myself. The functions in this asset really should be the bare minimum for string handling functions out of the box!
     
    Last edited: Jul 16, 2019
    00.Archer and xDGameStudios like this.
  5. xDGameStudios

    xDGameStudios Member

    Joined:
    Sep 15, 2016
    Posts:
    585
    This could come in as a good addition for the GML 2019 update.
     
  6. 00.Archer

    00.Archer Member

    Joined:
    Jul 24, 2018
    Posts:
    56
    I'd love to see those getting implemented.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice