String.ahk, Array.ahk, Misc.ahk

Post your working scripts, libraries and tools.
aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 18 Mar 2023, 07:52

@Descolada

why you use

Code: Select all

RTrim(s, this)
instead of

Code: Select all

SubStr(s, 1, -StrLen(this))

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: String.ahk, Array.ahk, Misc.ahk

Post by Descolada » 18 Mar 2023, 11:07

@aliztori, I'm not sure where you got the line RTrim(s, this) since I can't find it in any of the libs, but I will assume you are talking about InsertLine. This is because I pretty much 1-on-1 converted it from String Things library by tidbits, and didn't look too deep into it.
SubStr(new, 1, -StrLen(delim)) can't be used, because Loop Parse only allows single-character delimiters. This means that SubStr(new, 1, -(line > count ? 2 : 1)) should work and I'll update the library accordingly.

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 18 Mar 2023, 13:01

Descolada wrote:
18 Mar 2023, 11:07
@aliztori, I'm not sure where you got the line RTrim(s, this) since I can't find it in any of the libs, but I will assume you are talking about InsertLine. This is because I pretty much 1-on-1 converted it from String Things library by tidbits, and didn't look too deep into it.
SubStr(new, 1, -StrLen(delim)) can't be used, because Loop Parse only allows single-character delimiters. This means that SubStr(new, 1, -(line > count ? 2 : 1)) should work and I'll update the library accordingly.
like concat in string library

Code: Select all

	
	static Concat(words*) {
		delim := this, s := ""
		for v in words
			s .= v . delim
		return SubStr(s,1,-StrLen(this))
	}
	

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 18 Mar 2023, 13:03

Descolada wrote:
18 Mar 2023, 11:07
@aliztori, I'm not sure where you got the line RTrim(s, this) since I can't find it in any of the libs, but I will assume you are talking about InsertLine. This is because I pretty much 1-on-1 converted it from String Things library by tidbits, and didn't look too deep into it.
SubStr(new, 1, -StrLen(delim)) can't be used, because Loop Parse only allows single-character delimiters. This means that SubStr(new, 1, -(line > count ? 2 : 1)) should work and I'll update the library accordingly.
like concat in string library

Code: Select all

	
	static Concat(words*) {
		delim := this, s := ""
		for v in words
			s .= v . delim
		return SubStr(s,1,-StrLen(this)) ;it would be Rtrim(s, this)
		
		
	}
	

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: String.ahk, Array.ahk, Misc.ahk

Post by Descolada » 18 Mar 2023, 14:07

@aliztori, test it with these inputs:
"d".Concat("ddd", "ddd")
"<br>".Concat("break", "br")

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 21 Mar 2023, 10:38

Descolada wrote:
18 Mar 2023, 14:07
@aliztori, test it with these inputs:
"d".Concat("ddd", "ddd")
"<br>".Concat("break", "br")
Yeah your right, good point
another question
How did you set this type of path for a library and what rules does it follow? I didn't read this anywhere

Code: Select all

#include ..\Lib\Misc.ahk
#include ..\Lib\Array.ahk

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: String.ahk, Array.ahk, Misc.ahk

Post by Descolada » 22 Mar 2023, 00:00

@aliztori, if you are referring to the double periods, then you can read about those from Microsofts' path naming rules.

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 22 Mar 2023, 20:37

Descolada wrote:
22 Mar 2023, 00:00
@aliztori, if you are referring to the double periods, then you can read about those from Microsofts' path naming rules.
thnks

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 22 Mar 2023, 20:41

@Descolada

String.ahk could include Format

Code: Select all

static Format(args*)  => Format(this, args*)

Code: Select all

job := "Programmer"
age := 20
"ali is {1} and his age is {2}".Format(job, age)

ntepa
Posts: 426
Joined: 19 Oct 2022, 20:52

Re: String.ahk, Array.ahk, Misc.ahk

Post by ntepa » 23 Mar 2023, 04:56

Any function can be used as a method:

Code: Select all

__ObjDefineProp  := Object.Prototype.DefineProp
__ObjDefineProp(Primitive.Prototype, "__Call", {
    Call:(this, name, params) => %name%(this, params*)
})

job := "Programmer"
age := 20
"ali is {1} and his age is {2}".Format(job, age).MsgBox()

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: String.ahk, Array.ahk, Misc.ahk

Post by Descolada » 23 Mar 2023, 05:56

@ntepa, although any function can be used as a method, it defeats the purpose of the object-oriented approach. Namely a string class should only contain string manipulation related properties and methods, so you can be certain that any method that can be called on a string is also valid to use with strings. "mystring".NumGet() isn't valid, whereas "ali is {1}".Format("a programmer") is.
A good example of this is IsDigit: if it were a property of strings, then users wouldn't be surprised when IsDigit(5) throws an error.

MsgBox I wouldn't include in a string library, because it isn't limited to just strings: Integers are also supported (and perhaps objects with the ToString() method in the future as well).

@aliztori, thanks, added it to the library.

aliztori
Posts: 119
Joined: 19 Jul 2022, 12:44

Re: String.ahk, Array.ahk, Misc.ahk

Post by aliztori » 25 Mar 2023, 10:11

@Descolada

Array.ahk lib can be include Rand method which returns a random value from the array

Code: Select all

	static Rand() {
		Arr := this
		randIndex := Random(1, Arr.Length)
		return Arr[randIndex]
	} 
fat arrow syntax
Spoiler

songdg
Posts: 557
Joined: 04 Oct 2017, 20:04

Re: String.ahk, Array.ahk, Misc.ahk

Post by songdg » 18 Sep 2023, 04:56

Hey pal, can WordWrap and LineWrap be adapted to other language?

Descolada
Posts: 1123
Joined: 23 Dec 2021, 02:30

Re: String.ahk, Array.ahk, Misc.ahk

Post by Descolada » 18 Sep 2023, 23:24

@songdg, adding Unicode support would mean properly handling Unicode graphemes and grapheme clusters. I have previously considered it, but it's a fairly complicated topic I'm not very interested in so currently I have no plans of doing that. Additionally there are some considerations:
1) Currently as far as I know there is no AHK v2 Unicode parsing libraries, which means everything should be written from scratch and that would be most likely a lot of research and work (although there are v1 libraries to base upon). Unicode graphemes are tricky and I've played with using RegEx to do that (see String.ULength), but RegEx is ultimately a fairly slow approach. Although this would probably be the simplest solution, as RegEx could be used to map the locations of grapheme clusters to an array (RegExMatch object), which would then contain all the necessary offsets to use in String.ahk basic functions.
2) To get a similar performance to String.ahk with normal characters, the implementation would probably require using MCode. There would need to be a very quick detection whether a string contains graphemes or not, which could be used to select the method of string manipulation. Also it should be able to map grapheme start locations to an array and return it, or do the transformations directly. This would require a lot of C/C++ and I'm not interested in doing that.

If you want to read more about how messy Unicode support can get: [url=https://mathiasbynens.be/notes/javascript-unicode#other-grapheme-clusters]Javascript has an Unicode problem[/c].

songdg
Posts: 557
Joined: 04 Oct 2017, 20:04

Re: String.ahk, Array.ahk, Misc.ahk

Post by songdg » 19 Sep 2023, 05:34

@Descolada
Thanks, really appreciated. I have been use OSDTIP_Pop viewtopic.php?t=76881#p333577 to show some information, it seems handle Unicode pretty good, so I previously assumed it couldn't be such a tricky problem.

Post Reply

Return to “Scripts and Functions (v2)”