Reputation: 107
I've been working on a script that requires you to enter a username that is always equal to or less than 32 characters. My goal is to add extra characters to the end of the string if it is less than 32 characters. For example, if I used the word "Taco", which is four characters, I'd need to add 28 filler characters to it to accomplish my goal.
Here's the code I have so far:
set localUN to "Taco"
set UNLength to length of localUN
set blanksToAdd to (32 - UNLength)
set authID to localUN
repeat blanksToAdd times
set authID to authID & "█"
end repeat
While this does work, it's a little slow to run, so I was hoping to find a better solution.
Thanks.
Upvotes: 0
Views: 73
Reputation: 3259
Seems plenty fast enough to me; are you sure that's where your bottleneck lies?
Were it in a performance-sensitive loop that's running >10,000 times, you could squeeze out a few more µs per-iteration with a premade slug, e.g.:
property padding : "████████████████████████████████"
set s to "Taco"
if length of s < 32 then set s to text 1 thru 32 of (s & padding)
return s
But for non performance critical tasks like user input, your original code is perfectly good and I wouldn't bother changing it. "Premature optimization is the root of all evil", and all that.
Upvotes: 1