From a1b950687051c32e26de8681b0ed639ad32adfb4 Mon Sep 17 00:00:00 2001 From: LAUN Wolfgang Date: Fri, 16 Jan 2004 13:29:26 +0000 Subject: [PATCH] format/write (version 2) Message-ID: Fixes and additions to formats: Improvement: NULL chars in picture line Bugfix: C<@*> shown in output if not alone on a line New feature: C<^*> for variable-width, one-line-at-a-time text Improvement: Diagnostic on C<@#> and C<~~> Bugfix: Segmentation fault on big numbers Improvement (maybe): Truncation of numbers produces misleading output Bugfix: "}" terminates format Bugfix: Error when copying non-UTF to UTF (EBCDIC only) p4raw-id: //depot/perl@22161 --- embed.fnc | 3 +- embed.h | 6 ++ form.h | 1 + pod/perldiag.pod | 6 ++ pod/perlform.pod | 195 ++++++++++++++++++++++++++++++++++++------------- pp_ctl.c | 169 ++++++++++++++++++++++++++----------------- proto.h | 3 +- t/op/write.t | 216 +++++++++++++++++++++++++++++++++++++++++++++++++++---- toke.c | 4 +- 9 files changed, 469 insertions(+), 134 deletions(-) diff --git a/embed.fnc b/embed.fnc index 4e5665e..5ec0d10 100644 --- a/embed.fnc +++ b/embed.fnc @@ -1085,7 +1085,8 @@ s |void* |docatch_body s |void* |vdocatch_body |va_list args #endif s |OP* |dofindlabel |OP *o|char *label|OP **opstack|OP **oplimit -s |void |doparseform |SV *sv +s |OP* |doparseform |SV *sv +sn |bool |num_overflow |NV value|I32 fldsize|I32 frcsize s |I32 |dopoptoeval |I32 startingblock s |I32 |dopoptolabel |char *label s |I32 |dopoptoloop |I32 startingblock diff --git a/embed.h b/embed.h index d3f3d97..b6a90ad 100644 --- a/embed.h +++ b/embed.h @@ -1524,6 +1524,9 @@ #define doparseform S_doparseform #endif #ifdef PERL_CORE +#define num_overflow S_num_overflow +#endif +#ifdef PERL_CORE #define dopoptoeval S_dopoptoeval #endif #ifdef PERL_CORE @@ -4018,6 +4021,9 @@ #define doparseform(a) S_doparseform(aTHX_ a) #endif #ifdef PERL_CORE +#define num_overflow S_num_overflow +#endif +#ifdef PERL_CORE #define dopoptoeval(a) S_dopoptoeval(aTHX_ a) #endif #ifdef PERL_CORE diff --git a/form.h b/form.h index 95d3c57..9cf271a 100644 --- a/form.h +++ b/form.h @@ -24,3 +24,4 @@ #define FF_BLANK 14 #define FF_MORE 15 #define FF_0DECIMAL 16 +#define FF_LINESNGL 17 diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 937e3a8..69c22d6 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3215,6 +3215,12 @@ expression compiler gave it. (P) A "can't happen" error, because safemalloc() should have caught it earlier. +=item Repeated format line will never terminate (~~ and @# incompatible) + +(F) Your format containes the ~~ repeat-until-blank sequence and a +numeric field that will never go blank so that the repetition never +terminates. You might use ^# instead. See L. + =item Reversed %s= operator (W syntax) You wrote your assignment operator backwards. The = must diff --git a/pod/perlform.pod b/pod/perlform.pod index fac7efc..6e104bb 100644 --- a/pod/perlform.pod +++ b/pod/perlform.pod @@ -29,8 +29,9 @@ Output record formats are declared as follows: FORMLIST . -If name is omitted, format "STDOUT" is defined. FORMLIST consists of -a sequence of lines, each of which may be one of three types: +If the name is omitted, format "STDOUT" is defined. A single "." in +column 1 is used to terminate a format. FORMLIST consists of a sequence +of lines, each of which may be one of three types: =over 4 @@ -48,29 +49,106 @@ An argument line supplying values to plug into the previous picture line. =back -Picture lines are printed exactly as they look, except for certain fields -that substitute values into the line. Each field in a picture line starts -with either "@" (at) or "^" (caret). These lines do not undergo any kind -of variable interpolation. The at field (not to be confused with the array -marker @) is the normal kind of field; the other kind, caret fields, are used -to do rudimentary multi-line text block filling. The length of the field -is supplied by padding out the field with multiple "E", "E", or "|" -characters to specify, respectively, left justification, right -justification, or centering. If the variable would exceed the width -specified, it is truncated. - -As an alternate form of right justification, you may also use "#" -characters (with an optional ".") to specify a numeric field. This way -you can line up the decimal points. With a "0" (zero) instead of the -first "#", the formatted number will be padded with leading zeroes if -necessary. If any value supplied for these fields contains a newline, -only the text up to the newline is printed. Finally, the special field -"@*" can be used for printing multi-line, nontruncated values; it -should appear by itself on a line. - -The values are specified on the following line in the same order as -the picture fields. The expressions providing the values should be -separated by commas. The expressions are all evaluated in a list context +Picture lines contain output field definitions, intermingled with +literal text. These lines do not undergo any kind of variable interpolation. +Field definitions are made up from a set of characters, for starting and +extending a field to its desired width. This is the complete set of +characters for field definitions: + + @ start of regular field + ^ start of special field + < pad character for left adjustification + | pad character for centering + > pad character for right adjustificat + # pad character for a right justified numeric field + 0 instead of first #: pad number with leading zeroes + . decimal point within a numeric field + ... terminate a text field, show "..." as truncation evidence + @* variable width field for a multi-line value + ^* variable width field for next line of a multi-line value + ~ suppress line with all fields empty + ~~ repeat line until all fields are exhausted + +Each field in a picture line starts with either "@" (at) or "^" (caret), +indicating what we'll call, respectively, a "regular" or "special" field. +The choice of pad characters determines whether a field is textual or +numeric. The tilde operators are not part of a field. Let's look at +the various possibilities in detail. + + +=head2 Text Fields + +The length of the field is supplied by padding out the field with multiple +"E", "E", or "|" characters to specify a non-numeric field with, +respectively, left justification, right justification, or centering. +For a regular field, the value (up to the first newline) is taken and +printed according to the selected justification, truncating excess characters. +If you terminate a text field with "...", three dots will be shown if +the value is truncated. A special text field may be used to do rudimentary +multi-line text block filling; see L for details. + + Example: + format STDOUT = + @<<<<<< @|||||| @>>>>>> + "left", "middle", "right" + . + Output: + left middle right + + +=head2 Numeric Fields + +Using "#" as a padding character specifies a numeric field, with +right justification. An optional "." defines the position of the +decimal point. With a "0" (zero) instead of the first "#", the +formatted number will be padded with leading zeroes if necessary. +A special numeric field is blanked out if the value is undefined. +If the resulting value would exceed the width specified the field is +filled with "#" as overflow evidence. + + Example: + format STDOUT = + @### @.### @##.### @### @### ^#### + 42, 3.1415, undef, 0, 10000, undef + . + Output: + 42 3.142 0.000 0 #### + + +=head2 The Field @* for Variable Width Multi-Line Text + +The field "@*" can be used for printing multi-line, nontruncated +values; it should (but need not) appear by itself on a line. A final +line feed is chomped off, but all other characters are emitted verbatim. + + +=head2 The Field ^* for Variable Width One-line-at-a-time Text + +Like "@*", this is a variable width field. The value supplied must be a +scalar variable. Perl puts the first line (up to the first "\n") of the +text into the field, and then chops off the front of the string so that +the next time the variable is referenced, more of the text can be printed. +The variable will I be restored. + + Example: + $text = "line 1\nline 2\nline 3"; + format STDOUT = + Text: ^* + $text + ~~ ^* + $text + . + Output: + Text: line 1 + line 2 + line 3 + + +=head2 Specifying Values + +The values are specified on the following format line in the same order as +the picture fields. The expressions providing the values must be +separated by commas. They are all evaluated in a list context before the line is processed, so a single list expression could produce multiple list elements. The expressions may be spread out to more than one line if enclosed in braces. If so, the opening brace must be the first @@ -83,29 +161,47 @@ means that, if, for example, the run-time environment happens to specify a German locale, "," will be used instead of the default ".". See L and L<"WARNINGS"> for more information. -Picture fields that begin with ^ rather than @ are treated specially. -With a # field, the field is blanked out if the value is undefined. For -other field types, the caret enables a kind of fill mode. Instead of an -arbitrary expression, the value supplied must be a scalar variable name -that contains a text string. Perl puts as much text as it can into the -field, and then chops off the front of the string so that the next time + +=head2 Using Fill Mode + +On text fields the caret enables a kind of fill mode. Instead of an +arbitrary expression, the value supplied must be a scalar variable +that contains a text string. Perl puts the next portion of the text into +the field, and then chops off the front of the string so that the next time the variable is referenced, more of the text can be printed. (Yes, this means that the variable itself is altered during execution of the write() -call, and is not returned.) Normally you would use a sequence of fields -in a vertical stack to print out a block of text. You might wish to end -the final field with the text "...", which will appear in the output if -the text was too long to appear in its entirety. You can change which -characters are legal to break on by changing the variable C<$:> (that's -$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a +call, and is not restored.) The next portion of text is determined by +a crude line breaking algorithm. You may use the carriage return character +(C<\r>) to force a line break. You can change which characters are legal +to break on by changing the variable C<$:> (that's +$FORMAT_LINE_BREAK_CHARACTERS if you're using the English module) to a list of the desired characters. -Using caret fields can produce variable length records. If the text -to be formatted is short, you can suppress blank lines by putting a -"~" (tilde) character anywhere in the line. The tilde will be translated -to a space upon output. If you put a second tilde contiguous to the -first, the line will be repeated until all the fields on the line are -exhausted. (If you use a field of the at variety, the expression you -supply had better not give the same value every time forever!) +Normally you would use a sequence of fields in a vertical stack associated +with the same scalar variable to print out a block of text. You might wish +to end the final field with the text "...", which will appear in the output +if the text was too long to appear in its entirety. + + +=head2 Suppressing Lines Where All Fields Are Void + +Using caret fields can produce lines where all fields are blank. You can +suppress such lines by putting a "~" (tilde) character anywhere in the +line. The tilde will be translated to a space upon output. + + +=head2 Repeating Format Lines + +If you put two contiguous tilde characters "~~" anywhere into a line, +the line will be repeated until all the fields on the line are exhausted, +i.e. undefined. For special (caret) text fields this will occur sooner or +later, but if you use a text field of the at variety, the expression you +supply had better not give the same value every time forever! (C +is a simple example that would work.) Don't use a regular (at) numeric +field in such lines, because it will never go blank. + + +=head2 Top of Form Processing Top-of-form processing is by default handled by a format with the same name as the current filehandle with "_TOP" concatenated to it. @@ -338,11 +434,8 @@ block structure of the program, and, for historical reasons, formats exist outside that block structure. See L for further discussion of locale handling. -Inside of an expression, the whitespace characters \n, \t and \f are -considered to be equivalent to a single space. Thus, you could think -of this filter being applied to each value in the format: - - $value =~ tr/\n\t\f/ /; +Within strings that are to be displayed in a fixed length text field, +each control character is substituted by a space. (But remember the +special meaning of C<\r> when using fill mode.) This is done to avoid +misalignment when control characters "disappear" on some output media. -The remaining whitespace character, \r, forces the printing of a new -line if allowed by the picture line. diff --git a/pp_ctl.c b/pp_ctl.c index 48e3618..fe6c9f6 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -369,15 +369,20 @@ PP(pp_formline) bool item_is_utf8 = FALSE; bool targ_is_utf8 = FALSE; SV * nsv = Nullsv; + OP * parseres = 0; + char *fmt; + bool oneline; if (!SvMAGICAL(tmpForm) || !SvCOMPILED(tmpForm)) { if (SvREADONLY(tmpForm)) { SvREADONLY_off(tmpForm); - doparseform(tmpForm); + parseres = doparseform(tmpForm); SvREADONLY_on(tmpForm); } else - doparseform(tmpForm); + parseres = doparseform(tmpForm); + if (parseres) + return parseres; } SvPV_force(PL_formtarget, len); if (DO_UTF8(PL_formtarget)) @@ -413,6 +418,7 @@ PP(pp_formline) case FF_LINEMARK: name = "LINEMARK"; break; case FF_END: name = "END"; break; case FF_0DECIMAL: name = "0DECIMAL"; break; + case FF_LINESNGL: name = "LINESNGL"; break; } if (arg >= 0) PerlIO_printf(Perl_debug_log, "%-16s%ld\n", name, (long) arg); @@ -519,6 +525,7 @@ PP(pp_formline) while (s < send) { if (*s == '\r') { itemsize = s - item; + chophere = s; break; } if (*s++ & ~31) @@ -558,6 +565,7 @@ PP(pp_formline) while (s < send) { if (*s == '\r') { itemsize = s - item; + chophere = s; break; } if (*s++ & ~31) @@ -648,7 +656,7 @@ PP(pp_formline) sv_catpvn_utf8_upgrade(PL_formtarget, s, arg, nsv); for (; t < SvEND(PL_formtarget); t++) { #ifdef EBCDIC - int ch = *t++ = *s++; + int ch = *t; if (iscntrl(ch)) #else if (!(*t & ~31)) @@ -678,7 +686,13 @@ PP(pp_formline) SvSETMAGIC(sv); break; + case FF_LINESNGL: + chopspace = 0; + oneline = TRUE; + goto ff_line; case FF_LINEGLOB: + oneline = FALSE; + ff_line: item = s = SvPV(sv, len); itemsize = len; if ((item_is_utf8 = DO_UTF8(sv))) @@ -687,20 +701,31 @@ PP(pp_formline) bool chopped = FALSE; gotsome = TRUE; send = s + len; + chophere = s + itemsize; while (s < send) { if (*s++ == '\n') { - if (s == send) { - itemsize--; + if (oneline) { chopped = TRUE; + chophere = s; + break; + } else { + if (s == send) { + itemsize--; + chopped = TRUE; + } else + lines++; } - else - lines++; } } SvCUR_set(PL_formtarget, t - SvPVX(PL_formtarget)); if (targ_is_utf8) SvUTF8_on(PL_formtarget); - sv_catsv(PL_formtarget, sv); + if (oneline) { + SvCUR_set(sv, chophere - item); + sv_catsv(PL_formtarget, sv); + SvCUR_set(sv, itemsize); + } else + sv_catsv(PL_formtarget, sv); if (chopped) SvCUR_set(PL_formtarget, SvCUR(PL_formtarget) - 1); SvGROW(PL_formtarget, SvCUR(PL_formtarget) + fudge + 1); @@ -710,46 +735,24 @@ PP(pp_formline) } break; + case FF_0DECIMAL: + arg = *fpc++; +#if defined(USE_LONG_DOUBLE) + fmt = (arg & 256) ? "%#0*.*" PERL_PRIfldbl : "%0*.*" PERL_PRIfldbl; +#else + fmt = (arg & 256) ? "%#0*.*f" : "%0*.*f"; +#endif + goto ff_dec; case FF_DECIMAL: - /* If the field is marked with ^ and the value is undefined, - blank it out. */ arg = *fpc++; - if ((arg & 512) && !SvOK(sv)) { - arg = fieldsize; - while (arg--) - *t++ = ' '; - break; - } - gotsome = TRUE; - value = SvNV(sv); - /* Formats aren't yet marked for locales, so assume "yes". */ - { - STORE_NUMERIC_STANDARD_SET_LOCAL(); #if defined(USE_LONG_DOUBLE) - if (arg & 256) { - sprintf(t, "%#*.*" PERL_PRIfldbl, - (int) fieldsize, (int) arg & 255, value); - } else { - sprintf(t, "%*.0" PERL_PRIfldbl, (int) fieldsize, value); - } + fmt = (arg & 256) ? "%#*.*" PERL_PRIfldbl : "%*.*" PERL_PRIfldbl; #else - if (arg & 256) { - sprintf(t, "%#*.*f", - (int) fieldsize, (int) arg & 255, value); - } else { - sprintf(t, "%*.0f", - (int) fieldsize, value); - } + fmt = (arg & 256) ? "%#*.*f" : "%*.*f"; #endif - RESTORE_NUMERIC_STANDARD(); - } - t += fieldsize; - break; - - case FF_0DECIMAL: + ff_dec: /* If the field is marked with ^ and the value is undefined, blank it out. */ - arg = *fpc++; if ((arg & 512) && !SvOK(sv)) { arg = fieldsize; while (arg--) @@ -758,31 +761,22 @@ PP(pp_formline) } gotsome = TRUE; value = SvNV(sv); + /* overflow evidence */ + if (num_overflow(value, fieldsize, arg)) { + arg = fieldsize; + while (arg--) + *t++ = '#'; + break; + } /* Formats aren't yet marked for locales, so assume "yes". */ { STORE_NUMERIC_STANDARD_SET_LOCAL(); -#if defined(USE_LONG_DOUBLE) - if (arg & 256) { - sprintf(t, "%#0*.*" PERL_PRIfldbl, - (int) fieldsize, (int) arg & 255, value); -/* is this legal? I don't have long doubles */ - } else { - sprintf(t, "%0*.0" PERL_PRIfldbl, (int) fieldsize, value); - } -#else - if (arg & 256) { - sprintf(t, "%#0*.*f", - (int) fieldsize, (int) arg & 255, value); - } else { - sprintf(t, "%0*.0f", - (int) fieldsize, value); - } -#endif + sprintf(t, fmt, (int) fieldsize, (int) arg & 255, value); RESTORE_NUMERIC_STANDARD(); } t += fieldsize; break; - + case FF_NEWLINE: f++; while (t-- > linemark && *t == ' ') ; @@ -3557,7 +3551,7 @@ PP(pp_leavetry) RETURNOP(retop); } -STATIC void +STATIC OP * S_doparseform(pTHX_ SV *sv) { STRLEN len; @@ -3573,14 +3567,15 @@ S_doparseform(pTHX_ SV *sv) U32 *linepc = 0; register I32 arg; bool ischop; - int maxops = 2; /* FF_LINEMARK + FF_END) */ + bool unchopnum = FALSE; + int maxops = 12; /* FF_LINEMARK + FF_END + 10 (\0 without preceding \n) */ if (len == 0) Perl_croak(aTHX_ "Null picture in formline"); /* estimate the buffer size needed */ for (base = s; s <= send; s++) { - if (*s == '\n' || *s == '\0' || *s == '@' || *s == '^') + if (*s == '\n' || *s == '@' || *s == '^') maxops += 10; } s = base; @@ -3613,8 +3608,12 @@ S_doparseform(pTHX_ SV *sv) case ' ': case '\t': skipspaces++; continue; - - case '\n': case 0: + case 0: + if (s < send) { + skipspaces = 0; + continue; + } /* else FALL THROUGH */ + case '\n': arg = s - base; skipspaces++; arg -= skipspaces; @@ -3670,8 +3669,12 @@ S_doparseform(pTHX_ SV *sv) *fpc++ = FF_FETCH; if (*s == '*') { s++; - *fpc++ = 0; - *fpc++ = FF_LINEGLOB; + *fpc++ = 2; /* skip the @* or ^* */ + if (ischop) { + *fpc++ = FF_LINESNGL; + *fpc++ = FF_CHOP; + } else + *fpc++ = FF_LINEGLOB; } else if (*s == '#' || (*s == '.' && s[1] == '#')) { arg = ischop ? 512 : 0; @@ -3689,6 +3692,7 @@ S_doparseform(pTHX_ SV *sv) *fpc++ = s - base; /* fieldsize for FETCH */ *fpc++ = FF_DECIMAL; *fpc++ = (U16)arg; + unchopnum |= ! ischop; } else if (*s == '0' && s[1] == '#') { /* Zero padded decimals */ arg = ischop ? 512 : 0; @@ -3707,6 +3711,7 @@ S_doparseform(pTHX_ SV *sv) *fpc++ = s - base; /* fieldsize for FETCH */ *fpc++ = FF_0DECIMAL; *fpc++ = (U16)arg; + unchopnum |= ! ischop; } else { I32 prespace = 0; @@ -3761,6 +3766,38 @@ S_doparseform(pTHX_ SV *sv) Safefree(fops); sv_magic(sv, Nullsv, PERL_MAGIC_fm, Nullch, 0); SvCOMPILED_on(sv); + + if (unchopnum && repeat) + DIE(aTHX_ "Repeated format line will never terminate (~~ and @#)"); + return 0; +} + + +STATIC bool +S_num_overflow(NV value, I32 fldsize, I32 frcsize) +{ + /* Can value be printed in fldsize chars, using %*.*f ? */ + NV pwr = 1; + NV eps = 0.5; + bool res = FALSE; + int intsize = fldsize - (value < 0 ? 1 : 0); + + if (frcsize & 256) + intsize--; + frcsize &= 255; + intsize -= frcsize; + + while (intsize--) pwr *= 10.0; + while (frcsize--) eps /= 10.0; + + if( value >= 0 ){ + if (value + eps >= pwr) + res = TRUE; + } else { + if (value - eps <= -pwr) + res = TRUE; + } + return res; } static I32 diff --git a/proto.h b/proto.h index 3a14f7f..359c986 100644 --- a/proto.h +++ b/proto.h @@ -1042,7 +1042,8 @@ STATIC void* S_docatch_body(pTHX); STATIC void* S_vdocatch_body(pTHX_ va_list args); #endif STATIC OP* S_dofindlabel(pTHX_ OP *o, char *label, OP **opstack, OP **oplimit); -STATIC void S_doparseform(pTHX_ SV *sv); +STATIC OP* S_doparseform(pTHX_ SV *sv); +STATIC bool S_num_overflow(NV value, I32 fldsize, I32 frcsize); STATIC I32 S_dopoptoeval(pTHX_ I32 startingblock); STATIC I32 S_dopoptolabel(pTHX_ char *label); STATIC I32 S_dopoptoloop(pTHX_ I32 startingblock); diff --git a/t/op/write.t b/t/op/write.t index ad596a6..6e37cac 100755 --- a/t/op/write.t +++ b/t/op/write.t @@ -5,12 +5,59 @@ BEGIN { @INC = '../lib'; } -print "1..50\n"; +#-- testing numeric fields in all variants (WL) + +sub swrite { + my $format = shift; + local $^A = ""; # don't litter, use a local bin + formline( $format, @_ ); + return $^A; +} + +my @NumTests = ( + [ '@###', 0, 1, 9999.5, 9999.4999, -999.5, 1e100 ], + [ '@0##', 0, 1, 9999.5, -999.4999, -999.5, 1e100 ], + [ '^###', 0, undef ], + [ '^0##', 0, undef ], + [ '@###.', 0, 1, 9999.5, 9999.4999, -999.5 ], + [ '@##.##', 0, 1, 999.995, 999.99499, -100 ], + [ '@0#.##', 0, 1, 10, -0.0001 ], + ); + +sub mkfmt($){ + my $fmt = shift(); + my $fieldwidth = length( $fmt ); + my $leadzero = $fmt =~ /^.0/ ? "0" : ""; + if( $fmt =~ /\.(#*)/ ){ + my $fractwidth = length( $1 ); + return "%#${leadzero}${fieldwidth}.${fractwidth}f" + } else { + return "%${leadzero}${fieldwidth}.0f" + } +} + +my $num_tests = 0; +for my $tref ( @NumTests ){ + $num_tests += @$tref - 1; +} +#--------------------------------------------------------- + +# number of tests in section 1 +my $bas_tests = 20; + +# number of tests in section 3 +my $hmb_tests = 36; + +printf "1..%d\n", $bas_tests + $num_tests + $hmb_tests; my $CAT = ($^O eq 'MSWin32' || $^O eq 'NetWare' || $^O eq 'VMS') ? 'type' : ($^O eq 'MacOS') ? 'catenate' : 'cat'; +############ +## Section 1 +############ + format OUT = the quick brown @<< $fox @@ -274,14 +321,19 @@ else { my $el; - format STDOUT = + format OUT12 = ok ^<<<<<<<<<<<<<<~~ # sv_chop() naze $el . my %hash = (12 => 3); + open(OUT12, '>Op_write.tmp') || die "Can't create Op_write.tmp"; + for $el (keys %hash) { - write; + write(OUT12); } + close OUT12 or die "Could not close: $!"; + print `$CAT Op_write.tmp`; + } { @@ -300,24 +352,162 @@ $v print `$CAT Op_write.tmp`; } -{ - # Bug #24774 format without trailing \n failed assertion - # but this must not compile because we'd get a ';' into the format - +{ # test 14 + # Bug #24774 format without trailing \n failed assertion, but this + # must fail since we have a trailing ; in the eval'ed string (WL) my @v = ('k'); eval "format OUT14 = \n@\n\@v"; print $@ ? "ok 14\n" : "not ok 14\n"; } -####################################### -# Easiest to add new tests above here # +{ # test 15 + # text lost in ^<<< field with \r in value (WL) + my $txt = "line 1\rline 2"; + format OUT15 = +^<<<<<<<<<<<<<<<<<< +$txt +^<<<<<<<<<<<<<<<<<< +$txt +. + open(OUT15, '>Op_write.tmp') || die "Can't create Op_write.tmp"; + write(OUT15); + close OUT15 or die "Could not close: $!"; + my $res = `$CAT Op_write.tmp`; + print $res eq "line 1\nline 2\n" ? "ok 15\n" : "not ok 15\n"; +} + +{ # test 16: multiple use of a variable in same line with ^< + my $txt = "this_is_block_1 this_is_block_2 this_is_block_3 this_is_block_4"; + format OUT16 = +^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<< +$txt, $txt +^<<<<<<<<<<<<<<<< ^<<<<<<<<<<<<<<<< +$txt, $txt +. + open(OUT16, '>Op_write.tmp') || die "Can't create Op_write.tmp"; + write(OUT16); + close OUT16 or die "Could not close: $!"; + my $res = `$CAT Op_write.tmp`; + print $res eq <Op_write.tmp') || die "Can't create Op_write.tmp"; + write(OUT17); + close OUT17 or die "Could not close: $!"; + my $res = `$CAT Op_write.tmp`; + chomp( $txt ); + my $exp = <Op_write.tmp') || die "Can't create Op_write.tmp"; + eval { write(OUT18); }; + print $@ ? "ok 18\n" : "not ok 18\n"; + close OUT18 or die "Could not close: $!"; +} + +{ # test 19: \0 in an evel'ed format, doesn't cause empty lines (WL) + my $v = 'gaga'; + eval "format OUT19 = \n" . + '@<<<' . "\0\n" . + '$v' . "\n" . + '@<<<' . "\0\n" . + '$v' . "\n.\n"; + open(OUT19, '>Op_write.tmp') || die "Can't create Op_write.tmp"; + write(OUT19); + my $res = `$CAT Op_write.tmp`; + print $res eq < 'xval', ykey => 'yval' ); + format OUT20 = +@>>>> @<<<< ~~ +each %h +@>>>> @<<<< +$h{xkey}, $h{ykey} +@>>>> @<<<< +{ $h{xkey}, $h{ykey} +} +} +. + my $exp = ''; + while( my( $k, $v ) = each( %h ) ){ + $exp .= sprintf( "%5s %s\n", $k, $v ); + } + $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} ); + $exp .= sprintf( "%5s %s\n", $h{xkey}, $h{ykey} ); + $exp .= "}\n"; + open(OUT20, '>Op_write.tmp') || die "Can't create Op_write.tmp"; + write(OUT20); + my $res = `$CAT Op_write.tmp`; + print $res eq $exp ? "ok 20\n" : "not ok 20 res=[$res]exp=[$exp]\n"; + +EOD +} + + +##################### +## Section 2 +## numeric formatting +##################### + +my $nt = $bas_tests; +for my $tref ( @NumTests ){ + my $writefmt = shift( @$tref ); + my $printfmt = mkfmt( $writefmt ); + my $blank_when_undef = substr( $writefmt, 0, 1 ) eq '^'; + for my $val ( @$tref ){ + my $writeres = swrite( $writefmt, $val ); + my $printres; + if( $blank_when_undef && ! defined($val) ){ + $printres = ' ' x length( $writefmt ); + } else { + $printres = sprintf( $printfmt, $val || 0 ); + if( length($printres) > length( $writefmt ) ){ + $printres = '#' x length( $writefmt ); + } + } + $nt++; + + print $printres eq $writeres ? "ok $nt\n" : "not ok $nt\n"; + } +} + + +##################################### +## Section 3 +## Easiest to add new tests above here ####################################### -# 15..50: scary format testing from Merijn H. Brand +# scary format testing from H.Merijn Brand -my $test = 15; -my $tests = 50; +my $test = $bas_tests + $num_tests + 1; +my $tests = $bas_tests + $num_tests + $hmb_tests; if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'dos' || $^O eq 'MacOS' || ($^O eq 'os2' and not eval '$OS2::can_fork')) { @@ -351,7 +541,7 @@ $= = 10; ? "ok $test # TODO\n" : "not ok $test # TODO \$- = $- instead of 9\n"; $test++; print $^ ne "Comment_TOP" - ? "ok $test\n" : "not ok $test\n# \$^ = $^ instead of 'STDOUT_TOP'\n"; + ? "ok $test\n" : "not ok $test # TODO \$^ = $^ instead of 'STDOUT_TOP'\n"; $test++; } diff --git a/toke.c b/toke.c index da71929..5e4c7e7 100644 --- a/toke.c +++ b/toke.c @@ -7617,7 +7617,7 @@ S_scan_formline(pTHX_ register char *s) bool eofmt = FALSE; while (!needargs) { - if (*s == '.' || *s == /*{*/'}') { + if (*s == '.') { /*SUPPRESS 530*/ #ifdef PERL_STRICT_CR for (t = s+1;SPACE_OR_TAB(*t); t++) ; @@ -7630,7 +7630,7 @@ S_scan_formline(pTHX_ register char *s) } } if (PL_in_eval && !PL_rsfp) { - eol = strchr(s,'\n'); + eol = memchr(s,'\n',PL_bufend-s); if (!eol++) eol = PL_bufend; } -- 1.8.3.1