std-make-string


Synopsis

(std-make-string <n> <def>)

Description

STD-MAKE-STRING creates a string of n characters which are initialized to def, similar to STD-MAKE-LIST.

The string is made up of consecutive portions of the string-representation of def and then cut to n characters.

If def is a function then the consecutive results of evaluating def are gathered to make the string. def is called until the string has at least n characters. The string of length n is returned then, but def is not called n times and converted to a string of length n*(strlen (def)) So you cannot generally know how often def is called. See side effects below.

E.g. use may it to create a string of random characters.

Note

The order is different to STD-MAKE-LIST:

STD-MAKE-STRING puts each new element to the end of the string, STD-MAKE-LIST to the front!

Examples

static defaults:

(std-make-string 5 "x")	=> "xxxxx"
(std-make-string 10 "=")	=> "=========="
(std-make-string 3 nil)	=> "nil"
(std-make-string 3 10000)	=> "100"

dynamic defaults:

(std-make-string 10 '(lambda ()(chr (+ 65 (std-random 26))))
=> "HRLKZUEWAZ"
; a string of random upcase chars

(setq i -1)
(std-make-string 10 '(lambda ()(setq i (1+ i))))=> "0123456789"

But:

(std-make-string 2 '(- 2 1)) => "(-"

and not: "11" because (- 2 1) is no function, just a list.

use (std-make-string 2 '(lambda () (- 2 1))) instead to get

=> "11"

Arguments

n: a positive integer.

def: any valid lisp expression. If it is a function or lambda list the function is guaranteed to be freshly called for every single entry from the front to the back.

Return Value

A string of n characters.

Side Effects

def is evaluated between 1 and n times if it is a function. The exact number is not predictable, because the loop stops when the string reached the length n or more.

So avoid functions with side-effects!

Module

(std-require 'STDSTR)

Defined in STDSTR