📄 modifier.cpp
字号:
// is greater than the length of the descriptor
// src).
// src is copied into target field, aligned left
// and padded with '@' characters.
//
// length of buf becomes the same as the
// specified width, i.e 16
buf.Justify(src,16,ELeft,'@');
console->Printf(KFormatBufLenSizeMax,
&buf,
buf.Length(),
buf.Size(),
buf.MaxLength()
);
// target field in buf has width 16 (NB this
// is greater than the length of the descriptor
// src).
// src is copied into target field, aligned centre
// and padded with '@' characters
//
// length of buf becomes the same as the
// specified width, i.e 16
buf.Justify(src,16,ECenter,'@');
console->Printf(KFormatBufLenSizeMax,
&buf,
buf.Length(),
buf.Size(),
buf.MaxLength()
);
// target field in buf has width 10 (NB this
// is smaller than the length of the descriptor
// src).
// src is copied into target field but truncated
// to 10 characters and, therefore, alignment and
// padding information not used.
//
// length of buf becomes the same as the
// width, i.e 10
buf.Justify(src,10,ECenter,'@');
console->Printf(KFormatBufLenSizeMax,
&buf,
buf.Length(),
buf.Size(),
buf.MaxLength()
);
// target field in buf is set to the length of
// the descriptor src (whatever it currently is)
//
// src copied into target field. No padding and
// no truncatiuon needed and so the
// alignment and padding information is not used.
//
// length of buf becomes the same as the length
// of src i.e 12
buf.Justify(src,KDefaultJustifyWidth,ECenter,'@');
console->Printf(KFormatBufLenSizeMax,
&buf,
buf.Length(),
buf.Size(),
buf.MaxLength()
);
// implied width > maximum length of buf.
//
// Remove the "//" marks on the next two lines
// to see this panic
//_LIT(KTxtPanicCausingText,"Panic causing text because length > 32");
//src = KTxtPanicCausingText;
//buf.Justify(src,KDefaultJustifyWidth,ECenter,'@' );
// explicit width > maximum length of buf
//
// Remove the "//" marks on the next line
// to see this panic
//buf.Justify(src,33,ECenter,'@');
// AppendJustify() is similar but target
// field appended to descriptor.
//
// Target field in buf has width 16
// (NB greater than length of descriptor
// src) and is appended to existing content
// of buf.
// src copied into target field,
// aligned left and padded with '@'
// characters
//
// Resulting length of buf is the length
// of "ABCD" plus the width of the target
// field (16) giving a value of 20.
_LIT(KTextABCD,"ABCD");
buf = KTextABCD;
buf.AppendJustify(src,16,ELeft,'@');
console->Printf(KFormatBufLenSizeMax,
&buf,
buf.Length(),
buf.Size(),
buf.MaxLength()
);
//
// Format() & AppendFormat() * * *
//
_LIT(KTitleFormatAndAppendFormat,"\n--->Format() & AppendFormat()");
console->Printf(KTitleFormatAndAppendFormat);
console->Printf(KPressAnyKeyToContinue);
console->Getch();
// use tgt as a target descriptor to show
// the results of format()
TBuf<128> tgt;
// The basic %<type> format.
// Interprets the arguments as signed/unsigned
// integers and generates the appropriate
// character representations. The characters only
// occupy the space needed to represent them.
//
// Generates:"1000001 A 65 101 65 41"
//
// format string is:
// "%b %c %d %o %u %x"
_LIT(KFormat1,"%b %c %d %o %u %x");
tgt.Format(KFormat1,65,65,65,65,65,65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interprets the argument as an unsigned
// integer; the generated (hex) characters are
// right aligned and padded to the left
// with '0' to make a total of 4 characters.
//
// Generates:"0041"
//
// format string is:
// "%04x"
_LIT(KFormat2,"%04x");
tgt.Format(KFormat2,65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interprets the argument as an unsigned
// integer;the generated (hex) characters are
// right aligned and padded to the left with
// blanks to make a total of 4 characters.
//
// Generates:" 41"
//
// format string is:
// "%4x"
tgt.Format(KFormatfourex,65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interprets the argument as an unsigned
// integer; the generated (hex) characters are
// right aligned and padded to the left with
// blanks to make a total of 4 characters.
//
// Generates:"1fff"
//
// NB the correct hex value is "1ffff"
// but width specified as 4 resulting
// in TRUNCATION.
//
//
// format string is:
// "%4x"
tgt.Format(KFormatfourex,131071);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// The '*' means that the width of the output
// is taken from the 1st argument in the list;
// Interprets the 2nd argument as an unsigned
// integer.
// The generated (hex) characters are right
// aligned and padded to the left with blanks
// to make a total of 4 characters.
//
// Generates:" 41"
//
// format string is:
// "%*x"
_LIT(KFormat4,"%*x");
tgt.Format(KFormat4,4,65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interprets the 1st argument as a signed
// integer.
//
// The generated (decimal) characters are
// right aligned and padded to the left
// with '$' to make a total of 4 characters.
//
// These are then followed by the
// 4 characters ".00 "
//
// Interprets the 2nd argument as a descriptor.
// The characters within the descriptor are
// appended to those already generated.
//
// Generates:"$$65.00 over"
//
// Format string is:
// "%+$4d.00 %S"
//
_LIT(KTextover,"over");
_LIT(KFormat5,"%+$4d.00 %S");
tgt.Format(KFormat5,65,&KTextover);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// The * means that the width of the
// output is taken from the 1st argument
// in the list.
//
// Interprets the 2nd argument as a descriptor.
// The characters copied from it are right
// aligned and padded on the left with
// '0' characters
//
// Generates:"000000fred"
//
// Format string is:
// "%+0*S"
//
_LIT(KTextfred,"fred");
_LIT(KFormat6,"%+0*S");
tgt.Format(KFormat6,10,&KTextfred);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// The * means that the fill character is
// taken from the 1st argument in the list.
//
// Interprets the 2nd argument as an unsigned
// integer.
// The generated characters are centrally
// aligned and padded on the left and right
// with '*' to make 6 characters.
//
// Generates:"**41**"
//
// Format string is:
// "%=*6x"
//
_LIT(KFormat7,"%=*6x");
tgt.Format(KFormat7,'*',65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// The 1st '*' means that the fill character
// is taken from the 1st argument in the list.
//
// The 2nd '*' means that the width is taken
// from the 2nd argument in the list
//
// Interprets the 3rd argument as a signed
// integer.
// The generated characters are right aligned
// and padded on the left with '.' to make
// 10 characters.
//
// Generates:".......-65"
//
// Format string is:
// "%+**d"
//
_LIT(KFormat8,"%+**d");
tgt.Format(KFormat8,'.',10,(-65));
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Same as the previous example but the
// characters are left aligned and padded to
// the right with '.' characters.
//
// Generates:"-65......."
//
// Format string is:
// "%-**d"
//
_LIT(KFormat9,"%-**d");
tgt.Format(KFormat9,'.',10,(-65));
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Generates 6 fill characters 'A'.
// Makes no use of the argument list.
//
// Generates: "AAAAAA"
//
// Format string is:
// "%-A6p"
//
_LIT(KFormat10,"%-A6p");
tgt.Format(KFormat10,65);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interpret the argument as a TInt64 type.
_LIT(KFormat10a,"%Li");
TInt64 var(65);
tgt.Format(KFormat10a,var);
console->Printf(KFormatBufLenSizeMax,
&tgt,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interpret the argument as an unsigned integer
// and convert to a two byte numeric
// representation (most significant byte first)
//
// So 4660 = 0x1234 => 1st byte contains 0x12
// 2nd byte contains 0x34
//
// NB This is same for both ASCII & UNICODE build
//
// Format string is:
// "%m"
//
_LIT(KFormat11,"%m");
tgt.Format(KFormat11,4660);
counter = tgt.Length();
for (index = 0; index < counter; index++)
console->Printf(KFormatCommon,tgt[index]);
console->Printf(KFormatLenSizeMax,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interpret the argument as an unsigned integer
// and convert to a four byte numeric
// representation (most significant byte first)
//
// So 4660 = 0x1234 => 1st byte contains 0x00
// 2nd byte contains 0x00
// 3rd byte contains 0x12
// 4th byte contains 0x34
// NB This is same for both ASCII & UNICODE build
//
// Format string is:
// "%M"
//
_LIT(KFormat12,"%M");
tgt.Format(KFormat12,4660);
counter = tgt.Length();
for (index = 0; index < counter; index++)
console->Printf(KFormatCommon,tgt[index]);
console->Printf(KFormatLenSizeMax,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interpret the argument as an unsigned integer
// and convert to a two byte numeric
// representation (least significant byte first)
//
// So 4660 = 0x1234 => 1st byte contains 0x34
// 2nd byte contains 0x12
//
// NB This is same for both ASCII & UNICODE build
//
// Format string is:
// "%w"
//
_LIT(KFormat13,"%w");
tgt.Format(KFormat13,4660);
counter = tgt.Length();
for (index = 0; index < counter; index++)
console->Printf(KFormatCommon,tgt[index]);
console->Printf(KFormatLenSizeMax,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
// Interpret the argument as an unsigned integer
// and convert to a four byte numeric
// representation (least significant byte first)
//
// So 4660 = 0x1234 => 1st byte contains 0x34
// 2nd byte contains 0x12
// 3rd byte contains 0x00
// 4th byte contains 0x00
// NB This is same for both ASCII & UNICODE build
//
// Format string is:
// "%W"
//
_LIT(KFormat14,"%W");
tgt.Format(KFormat14,4660);
counter = tgt.Length();
for (index = 0; index < counter; index++)
console->Printf(KFormatCommon,tgt[index]);
console->Printf(KFormatLenSizeMax,
tgt.Length(),
tgt.Size(),
tgt.MaxLength()
);
console->Printf(KPressAnyKeyToContinue);
console->Getch();
//
// AppendFormat() illustrating use of the
// overflow handler
//
_LIT(KTitleOverflowHandler,"\n--->AppendFormat() & the overflow handler");
console->Printf(KTitleOverflowHandler);
console->Printf(KPressAnyKeyToContinue);
console->Getch();
// use the TestOverflow class as the overflow
// handler
TBuf<16> overtgt;
TestOverflow theoverflow;
// prime "overtgt" with data (of length 14)
_LIT(KTextabcdefghijklmn,"abcdefghijklmn");
overtgt = KTextabcdefghijklmn;
// Format string contains just literal
// text and no embedded commands.
// Length of literal text is 3
// 14+3 > max length of "overtgt" and
// so theoverflow.Overflow() will be invoked
_LIT(KTextopq,"opq");
overtgt.AppendFormat(KTextopq,&theoverflow);
// NB omitting the 2nd parameter, giving:
// overtgt.AppendFormat(KTextopq);
// results in a panic.
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -