limiting string output with %.ns

Q&A's, tips, howto's
Locked
nigelbrown
Posts: 429
Joined: Tue Nov 11, 2003 2:11 am
Location: Brisbane, Australia

limiting string output with %.ns

Post by nigelbrown »

In C I believe you can limit the maximum number of caharacters to be output from a string by using the precision part of the s format specifier.
eg see borland docs:
How [.prec] Affects Conversion

Char Type Effect of [.prec] (.n) on Conversion

d Specifies that at least n digits are printed.
i If input argument has less than n digits,
o output value is left-padded x with zeros.
u If input argument has more than n digits,
x the output value is not truncated.
X

e Specifies that n characters are
E printed after the decimal point, and
f the last digit printed is rounded.

g Specifies that at most n significant

G digits are printed.

c Has no effect on the output.
s Specifies that no more than n characters are printed.

Newlisp doesn't recognise this (and doesn't claim to)
viz
> (format "%4s" "hello")
"hello"
> (format "%.4s" "hello")

problem in format string in function format : "%.4s"

> (format "%5.4s" "hello")

problem in format string in function format : "%5.4s"

> (format "%4.4s" "hello")

problem in format string in function format : "%4.4s"

>
so there is no way within the format specification to limit string output size (it could be done by in the variable by slicing the string but it would be better to do it in the format I believe).

Could %s for format be easily extended that way?
I've put this in the win32 thread as I've not looked at the behaviour on linux.

Regards
Nigel

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Currently 'format' in newlisp verifies only the most common subset for all possible formats of the 'printf()' 'C' function. The precision spec after the .(dot) is only accepted for floating point precision.

I think I can expand this for strings in an upcoming release.


Lutz

Locked