From db0d4048b30f61ecdb1b4ca2626efd500002c2e6 Mon Sep 17 00:00:00 2001 From: omicronrex <65692652+omicronrex@users.noreply.github.com> Date: Wed, 28 Feb 2024 16:36:03 -0300 Subject: [PATCH] string_trim() --- gm82core.gej | 59 ++++++++++++++++++++++++++-------------------- source/strings.gml | 38 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 25 deletions(-) diff --git a/gm82core.gej b/gm82core.gej index f5b4a98..cb2689f 100644 --- a/gm82core.gej +++ b/gm82core.gej @@ -3,7 +3,7 @@ "folder": "gm82", "version": "1.6", "author": "renex, Floogle, Lovey01, cam900, Adamcake, YellowAfterlife, Verve", - "date": "07/02/2024", + "date": "28/02/2024", "license": "Free to use, also for commercial games.", "description": "Part of the standard Game Maker 8.2 distribution. This extension package contains a number of helper functions that are commonly used, and also geometry functions introduced in GM:Studio. This extension is also required for using the other GM 8.2 extensions, Sound and Joystick.", "helpfile": "", @@ -2053,6 +2053,30 @@ ], "returntype": 2 }, + { + "name": "string_copy_end", + "extname": "", + "calltype": 2, + "helpline": "string_copy_end(string,count)", + "hidden": false, + "argtypes": [ + 2, + 2 + ], + "returntype": 2 + }, + { + "name": "string_delete_end", + "extname": "", + "calltype": 2, + "helpline": "string_delete_end(string,count)", + "hidden": false, + "argtypes": [ + 2, + 2 + ], + "returntype": 2 + }, { "name": "string_ends_with", "extname": "", @@ -2123,6 +2147,15 @@ ], "returntype": 2 }, + { + "name": "string_trim", + "extname": "", + "calltype": 2, + "helpline": "string_trim(string,[trim1,trim2...])", + "hidden": false, + "argtypes": null, + "returntype": 2 + }, { "name": "strong", "extname": "str_cat", @@ -2158,30 +2191,6 @@ "hidden": false, "argtypes": null, "returntype": 2 - }, - { - "name": "string_delete_end", - "extname": "", - "calltype": 2, - "helpline": "string_delete_end(string,count)", - "hidden": false, - "argtypes": [ - 2, - 2 - ], - "returntype": 2 - }, - { - "name": "string_copy_end", - "extname": "", - "calltype": 2, - "helpline": "string_copy_end(string,count)", - "hidden": false, - "argtypes": [ - 2, - 2 - ], - "returntype": 2 } ], "constants": [] diff --git a/source/strings.gml b/source/strings.gml index c3eadb6..b313809 100644 --- a/source/strings.gml +++ b/source/strings.gml @@ -178,5 +178,43 @@ //returns: copy from provided string return string_copy(argument0, max(string_length(argument0) - argument1 + 1, 1), argument1); + + +#define string_trim + ///string_trim(string,[trim1,trim2...]) + //string: string to process + //arguments: strings to trim out + //returns: trimmed string + //This function will remove occurrences of the trims from the edges of a string. + //If no extra arguments are specified, spaces are removed instead. + + var __i,__p1,__p2,__str,__trim,__trimlen; + + __str=argument[0] + __i=1 + + if (argument_count==1) { + __j=string_length(__str) + + while (string_char_at(__str,__i)==" " && __i<__j) __i+=1 + while (string_char_at(__str,__j)==" " && __j>0) __j-=1 + + return string_copy(__str,__i,__j-__i+1) + } + + repeat (argument_count-1) { + __trim=argument[__i] + + __trimlen=string_length(__trim) + while (string_copy(__str,1,__trimlen)==__trim) { + __str=string_delete(__str,1,__trimlen) + } + + while (string_copy(__str,string_length(__str)+1-__trimlen,__trimlen)==__trim) { + __str=string_delete(__str,string_length(__str)+1-__trimlen,__trimlen) + } + __i+=1} + + return __str // // \ No newline at end of file