Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

fill() - Functions to get a string filled with string


  • Please log in to reply
8 replies to this topic
Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Online Documentation

Download Offline Documentation
(includes source of script)

fill_String() - Sometimes, we need a string filled with specific count of characters. Here is my solution. I decided to use a loop construct against VarSetCapacity() and NumGet() function calls, because small values are a bit faster. Also, with a loop, we can pad any string with any length and are not restricted to one characters.

If you want to get just a specific count of source string without a base string where to add, then fill() works faster.

If you want to get just a specific count of a single character instead of a string any lenght, then fill_Char() works very fast on high amount of copies.

; Pad string with 3 leading zeros.
MsgBox % fill(3) . 123 ; Default value is zero ('0').
; >> Returns "000123"

; Add spaces at one character before end of string.
MsgBox % fill_String(5, " ", "Hello World!", -1)
; >> Returns "Hello World     !"

It is ready for stdlib, just save as fill.ahk.

garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
thank you Tuncay,
I don't understand functions, can you give an example how I can use variable ?
VAR1=12345   ;  > "   12345.00"    example, fill with 3 spaces or with 3 leading  zeros
VAR2=123.7   ;  > "     123.70"           ,           5                5


majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
There is much much faster version of this function for single chars, and arbitrary length:


msgBox % StrPad("some text ", "-", 20)

StrPad(str, padchar, padlen, left=1){
 	if (i := padlen-StrLen(str))
		VarSetCapacity(w, i, asc(padchar)),  NumPut(0, &w+i, "Char"),  VarSetCapacity(w, -1)
	return left ? w str : str w
}

Acctually, it seems that your version is a little bit faster for smaller padding values but for larger values, it is very very slow.


t := A_TickCount
loop, 10000
	StrPad("some text ", "-", 2000)
t1 := A_TickCount - t

t := A_TickCount
loop, 10000
	String_fill(2000, " ", "some text ")
t2 := A_TickCount - t

msgbox %t1% %t2%

20: 282 218
100: 282 906
200: 531 1938
2000: 625 64719
Posted Image

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
majkinetor, thx for the comparison. I did not search for such a function. I did compare a version with VarSetCapacity() against a version with a Loop construct, and end up with Loop, because tested on smaller files.

Note that there are some advantages and options, like padding more than 1 character and the ability to add that on any position of final string (if any is specified). Also, instead to define how many characters (or copies of to added string), the max length of final string can be specified.

Note also that there are faster versions named String_fillLeft() and String_fillRight() without some options.

Its documented, and there are examples. Please look in source.

AR1=12345   ;  > "   12345.00"    example, fill with 3 spaces or with 3 leading  zeros

The function does not add any dot. Here is an example how to:
#include E:\home\Aktuelle Projekte\Current\String_fill\string_fill.ahk
x=12345   ;  > "   12345.00"    example, fill with 3 spaces or with 3 leading  zeros
x := string_fillLeft(3, " ", x)
x := string_fillRight(2, 0, x . ".")
MsgBox "%x%"
Currently, the String_FillRight() and -Left () are not stdlib compliant and so, it must be included.

VAR2=123.7   ;  > "     123.70"

Try this:
#include E:\home\Aktuelle Projekte\Current\String_fill\string_fill.ahk
x=123.7   ;  > "     123.70"
x := string_fillLeft(5, " ", x) . string_fillRight() ; default values on fillright: get one zero
MsgBox "%x%"


garry
  • Spam Officer
  • 3219 posts
  • Last active: Sep 20 2018 02:47 PM
  • Joined: 19 Apr 2005
thank you Tuncay for the explanation, now I see how use the function

majkinetor
  • Moderators
  • 4512 posts
  • Last active: Jul 29 2016 12:40 AM
  • Joined: 24 May 2006
Maybe you should make function separate cases with 1 char and with more chars.

Afterall, the highest probability is that the pad will have 1 char so this needs to be as fast as possible.

Thx.
Posted Image

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Source updated. Renamed and reworked functions. Now, the Left and Right ones are not available anymore. There are just fill() and fill_String(). They should work a little faster. Do have anyone some optimization ideas?

Maybe you should make function separate cases with 1 char and with more chars.

I fully understand you, but how should do I that?? The function works with Loop construct, adding just the source string on every iteration. That simple.

garry, after update, the new source would not work with old function calls anymore. Here are the examples above with the new ones.

x=12345   ;  > "   12345.00"    example, fill with 3 spaces or with 3 leading  zeros
x := fill(3, " ") . x . "." . fill(2)
MsgBox "%x%"
x=123.7   ;  > "     123.70"
x := fill(5, " ") . x . fill(1) ; fill() without second param defaults to '0'.
MsgBox "%x%"

Its stdlib compatible. Save the script as fill.ahk in standard library folder.

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Here is another update. Now functions added to get big copies of single characters.

New functions: fill_LC(), fill_Zero(), fill_Space()

Tuncay
  • Members
  • 1945 posts
  • Last active: Feb 08 2015 03:49 PM
  • Joined: 07 Nov 2006
Ok now it comes with an external documentation file. fill_FC is renamed to fill_Char(). The functions fill_Space() and fill_Zero() are removed, because they arent much faster than fill_Char().

Sorry if I confused someone with that. This is the last change for now.