slice string, pad characters

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

slice string, pad characters

10 Apr 2018, 21:55

- I've been preparing some function libraries to share. I thought I'd share this one particular function now.
- Btw, do make any suggestions re. whether it should be called StrPad or something else. Essentially the idea is to insert a string every n characters.

Code: Select all

;e.g.
;MsgBox, % JEE_StrHatch("abcdefghij", ",") ;a,b,c,d,e,f,g,h,i,j
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3) ;abc,def,ghi,j
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3, "R") ;a,bcd,efg,hij
;MsgBox, % JEE_StrHatch("abcdefghij", ",", 3, "O")[2] ;def

;vSep: separator
;vBlockLen: length of character blocks
;vOpt: R: from which end, start from right
;vOpt: O: return object (vSep is ignored)
;JEE_StrSlice
JEE_StrHatch(vText, vSep, vBlockLen:=1, vOpt:="")
{
	if (vText = "")
		return
	if InStr(vOpt, "R")
		vOffset := vBlockLen - Mod(StrLen(vText), vBlockLen)
	else
		vOffset := 0
	if InStr(vOpt, "O")
	{
		oArray := []
		oArray.SetCapacity(StrLen(vText)//vBlockLen)
		vTemp := ""
		Loop, Parse, vText
		{
			if (Mod(A_Index + vOffset, vBlockLen) = 0)
				oArray.Push(vTemp A_LoopField), vTemp := ""
			else
				vTemp .= A_LoopField
		}
		if !(vTemp = "")
			oArray.Push(vTemp)
		return oArray
	}
	VarSetCapacity(vOutput, StrLen(vText)*2+(StrLen(vText)//vBlockLen)*StrLen(vSep)*2)
	Loop, Parse, vText
	{
		if (Mod(A_Index + vOffset, vBlockLen) = 0)
			vOutput .= A_LoopField vSep, vDoCrop := 1
		else
			vOutput .= A_LoopField, vDoCrop := 0
	}
	if vDoCrop
		return SubStr(vOutput, 1, -StrLen(vSep))
	else
		return vOutput
}
[EDIT:] Renamed from 'StrPad' to 'StrHatch'. Another potential name is 'StrDelimit'.
[EDIT:] See a more appropriately named 'StrPad' function, lower down:
slice string, pad characters - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 05#p237105
Last edited by jeeswg on 06 Sep 2018, 18:15, edited 3 times in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: slice string, pad characters

11 Apr 2018, 20:10

StrPad is a horrible name. As is StrSlice. It does neither of those things.

Maybe StrDelimit?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

11 Apr 2018, 22:34

- Keep trying. Thanks.
- I don't like the word 'slice', but it does seen pretty apt.
- [EDIT:] Re. 'delimit'. Have you ever seen 'delimit' used in a function name?
- In Excel, text to columns (delimited), splits text, this adds characters (unless you use the object option).
- Some other ideas, but I need more ideas.
StrIntermix
StrInsert
StrIntersperse
StrInterlace
StrMingle
Stringle
Last edited by jeeswg on 13 Apr 2018, 15:57, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: slice string, pad characters

12 Apr 2018, 19:34

yes this is delimiting. Excel can split data by delimiters. Not sure where you researched your terms?

padding is adding characters to meet a certain length. such as padding 12 with 0's to 5 digits would be 00012

Slicing is slicing--which you do in AHK with SubStr()
try it and see
...
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 Apr 2018, 20:05

- Pad. If you look at the dictionary definition for the verb 'pad', it commonly means to fill/intersperse. On searching the Internet for 'pad' I noticed that it was often used in IT contexts to refer to adding characters to the beginning/end of a string. Hence why I asked for other ideas.
- Slice. I mentioned the word 'slice' because IIRC someone had written a similar function and used the word 'slice'. I've seen that JavaScript has 'slice', and that it's similar to AHK's SubStr, but do you have concrete examples form other programming languages to back up your claim?
- Crucially, has anyone seen an existing function that works like this one, to add a string every n characters?
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: slice string, pad characters

12 Apr 2018, 20:11

I doubt anyone has seen such a function before because no one has had the need for such functionality. And if they did, they didn’t feel like posting whatever solution they came up with.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 Apr 2018, 20:16

Do a Google search for: add string every n characters.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
derz00
Posts: 497
Joined: 02 Feb 2016, 17:54
Location: Middle of the round cube
Contact:

Re: slice string, pad characters

13 Apr 2018, 07:41

Alright, by elimination, pad isn't ideal.

About slice:
jeeswg wrote:I've seen that JavaScript has 'slice', and that it's similar to AHK's SubStr, but do you have concrete examples form other programming languages to back up your claim?
You're right; SubStr is slicing.
What claim did I make? Pardon my misunderstanding.

Here's how you slice in python. The for-loop is just to show that strings are also iterable, so you can index a string just like an array (called a list in Python)

Code: Select all

>>> from string import ascii_lowercase as alphabet
>>> alphabet[3]
'd'
>>> alphabet[0]
'a'
>>> alphabet[-1]
'z'
>>> for l in alphabet:
	print(l,end=' ')

a b c d e f g h i j k l m n o p q r s t u v w x y z 
>>> alphabet[0:9]
'abcdefghi'
>>>
And about delimit: I don't know what I'd use this function for except to delimit a string. It isn't something you usually do, hence this difficulty in naming it--there isn't a "real name" for it in the programming world.

I guess my point is just that this isn't padding or slicing, but that's as far as I can get with helping you with ideas. :)
try it and see
...
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: slice string, pad characters

13 Apr 2018, 08:05

intersperse seems to be the word we are looking for
Recommends AHK Studio
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

13 Apr 2018, 12:12

- @derz00. Uses.
- This kind of thing is useful for grouping things into 3 (numbers), or 2/4/8/16 (numbers/hex/lists of Unicode characters).
- It's also useful for splitting up hex for use with WinMerge, when comparing two files.
- And it's good for taking binary data stored as hex, and parsing a list of 4-byte (8-hex character) pixel ARGB/BGRA values.
- It's also useful for 'padding' a string with asterisks (e.g. inserting a string every character: .*) when doing RegEx searches.
- Also, efficiently splitting a string, starting from the right can be a bit fiddly.

- Re 'slice':
[CharBloat/StringSlice, similar to my function]
StringMod() String Manipulation - Enhanced by PhiLho / Titan - Page 3 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/7706 ... tan/page-3
[a different StringSlice, that works more like derz00 described]
Crazy Scripting : StringSlice() for FileToHex - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/5885 ... filetohex/

- You'd said that 'Slicing is slicing', which implies that everyone in the IT world is in agreement. I wasn't particularly familiar with it, I did a search and noticed a JavaScript reference.
- Like with all meta discussions, where I ask re. any precedents, it would help if you could mention explicitly any languages you know that use it. E.g. is the concept used in old/new languages, in well-known/little-known languages, in something that sees more widespread use like Excel VBA, batch files, html, or LaTeX.

- I'm not sure if 'StrDelimit' would have a clear meaning, or even be suggestive of anything. A good word would describe what it does.

- @nnnik: Hmm, StrIntersperse isn't bad, a bit long though.
- Ideally the word would be short, not start with 'in', because the 'in' words are a bit bland, and not start with 's'. Also, I didn't particularly like the word 'slice'.
- Some other ideas, although not necessarily great are:
StrPepper
StrSow (sow seeds at regular intervals)
- I tried looking up various words on Thesaurus.com:
insert, mix, suffuse, pepper, intersperse.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: slice string, pad characters

13 Apr 2018, 15:36

To insert at every n:th char, it is a simple regex. Maybe someone can do the "r" option with regex too? Putting aside that I might have made a mistake, the o option is not equivalent since it requires a delimiter,

Code: Select all

; v2
stringle(byref str, n, c := ",", opt := ""){
	return ( ( instr(opt, "r")	? (o:=mod(strlen(str),n), s:=substr(str,1,o), str:=substr(str,o+1)): "")
			 , str:=(s!=""?s c:"") regexreplace(str, "(.{" n "}(?!$))", "$0" c), instr(opt,"o") ? strsplit(str, c) : str )												
}
; Example
msgbox stringle("abcdefghij", 3,  "`n", "r")
Consider it a showcase of my dynamic style ;). Also note the choice of function name, my favourite suggestion :thumbup: .

Cheers, and thanks for sharing :) .
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

13 Apr 2018, 15:47

- Nicely done Helgef.
- Btw if the pad text contains $, there might be an issue when RegEx is used.
- One of the reasons I added in the object functionality, is for situations where the text might already include the pad text. So if you then split by the pad text, it might not work correctly.
- A StrRept function might be useful for handling splits from the right.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

12 May 2018, 16:14

- Some more ideas, using physical imagery as a guide:
StrHatch (like hatch marks on a ruler, a mark is made at regular intervals)
StrButton (like buttons on a shirt)
StrSpread

- It seems that 'intersperse', and 'intercalate', are used in Haskell and have a different meaning.
- I thought that 'intersperse' was a good candidate, despite being quite long. (Although since the function would be used rarely, length wouldn't be too much of a problem.)
- Links:
http://slideplayer.pl/12279891/72/image ... ście..jpg
http://images.slideplayer.com/27/891033 ... lide_6.jpg

- At the moment StrHatch looks like the best choice. Has that been used in any programming language?
- Normally choosing function names is relatively easy.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

05 Sep 2018, 23:50

STRHATCH

StrHatch. My current choice. Hatching means to add parallel lines when drawing, which is the only nearby word I could think of.

hatching - Wiktionary
https://en.wiktionary.org/wiki/hatching#English

STRDELIMIT

StrDelimit has a certain elegance. The only problem being that delimit does not necessarily imply regular intervals.

So if something like this was ever added to AutoHotkey, it could be a good candidate.

(The name surprised me a bit at first, because you usually hear of 'delimiters' in the context of text being divided into separate entities, instead of one item being modified, however, once the dust settles, the name can be appreciated.)

(Also StrAddDelimiters would be an alternative more descriptive name, but that would be too long, so StrDelimit would be better.)

delimit - Wiktionary
https://en.wiktionary.org/wiki/delimit
delimiter - Wiktionary
https://en.wiktionary.org/wiki/delimiter
Delimiter - Wikipedia
https://en.wikipedia.org/wiki/Delimiter
delimit | Hugo
https://gohugo.io/functions/delimit/

comma-separated list
pipe-separated list
delimiter-separated list

Excel: Data, Text to Columns, Delimited
Excel: Data, Text to Columns, Fixed width

If anyone ever does come across an existing function in a programming language that splits by every n characters, that would be great to know. This had even been suggested as a feature in an AHK v2 discussion.

AutoHotkey v2 Alpha Release - Page 32 - Scripts and Functions - AutoHotkey Community
https://autohotkey.com/board/topic/6531 ... ntry443330

STRPAD

I will use the name 'StrPad' for a function to add text to the start/end of string, a bit like the Format function.

Links to any existing such functions would be welcome.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

06 Sep 2018, 00:30

I've created a 'StrPad' function, for adding characters to the start/end of a string.

Code: Select all

;vNum: positive: pad with characters to the left
;vNum: negative: pad with characters to the right
;MsgBox, % JEE_StrPad("abc", 5, "-") ;--abc
;MsgBox, % JEE_StrPad("abc", -5, "-") ;abc--
;MsgBox, % JEE_StrPad("abc", 10, "ABC") ;ABCABCAabc
;MsgBox, % JEE_StrPad("abc", -10, "ABC") ;abcABCABCA

JEE_StrPad(vText, vLen, vFill:=" ")
{
	if (StrLen(vText) >= Abs(vLen))
		return vText
	Loop, % Ceil((Abs(vLen)-StrLen(vText))/StrLen(vFill))
		vFill2 .= vFill
	if (vLen > 0)
		return SubStr(vFill2, 1, vLen-StrLen(vText)) vText
	else
		return vText SubStr(vFill2, 1, Abs(vLen)-StrLen(vText))
}
Last edited by jeeswg on 06 Sep 2018, 18:18, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
burque505
Posts: 1734
Joined: 22 Jan 2017, 19:37

Re: slice string, pad characters

06 Sep 2018, 12:59

Thanks, jeeswg, that is a nice function.
Regards,
burque505
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

20 Sep 2018, 11:59

- @burque505: Cheers, no problem.

- I've been wondering about the best handling for multiple character fill strings i.e. fill from left-to-right or fill from right-to-left. Possibly an extra parameter would be needed.
- Are either of these useful, and which would be more useful as the default?
- One use for a fill string could be as a ruler, e.g. groups of 4 or 8 characters, to indicate where columns (i.e. when using tab characters) would start.

Code: Select all

which should be the default when adding text on the left?
[FILL][FILL][FI_origtext_ ;fill from left-to-right
LL][FILL][FILL]_origtext_ ;fill from right-to-left

which should be the default when adding text on the right?
_origtext_[FILL][FILL][FI ;fill from left-to-right
_origtext_LL][FILL][FILL] ;fill from right-to-left
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: slice string, pad characters

17 Dec 2018, 15:28

I've come up with a new word just now, 'inpend'. I don't plan to call the function that, but I would describe my 'StrHatch' function as 'inpending' at regular intervals. Cf. append and prepend.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 221 guests