{"id":103025,"date":"2019-10-25T07:00:00","date_gmt":"2019-10-25T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=103025"},"modified":"2019-10-25T03:24:36","modified_gmt":"2019-10-25T10:24:36","slug":"20191025-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20191025-00\/?p=103025","title":{"rendered":"Why does <CODE>Format&shy;Message<\/CODE> say that <CODE>%0<\/CODE> terminates the message without a trailing newline? Is it secretly <I>adding<\/I> newlines?"},"content":{"rendered":"<p>The documentation for the <code>Format\u00adMessage<\/code> function says that <code>%0<\/code> terminates the message without a trailing newline. Why does there need to be a special format to prevent it from adding a newline? Does it secretly <i>add<\/i> newlines to my format strings?<\/p>\n<p>Well, it&#8217;s not <code>Format\u00adMessage<\/code> that&#8217;s adding the newline.<\/p>\n<p>The <code>%0<\/code> sequence causes the <code>Format\u00adMessage<\/code> function to stop processing the format string, as if the format string had terminated right there, rather than wherever it actually does terminate.\u00b9<\/p>\n<p>But why does this feature need to exist in the first place? I mean, if you don&#8217;t want a newline at the end of the output, then don&#8217;t put a newline at the end of your format string.<\/p>\n<p>There are two ways to specify a format string to the <code>Format\u00adMessage<\/code> function. One is to pass the format string explicitly, in which case you can certainly control whether there is a newline at the end of the format string or not.<\/p>\n<p>The other is to ask that the format string come from a message resource. And that&#8217;s the case that the <code>%0<\/code> format specifier was intended for.<\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/desktop\/EventLog\/message-text-files\"> The syntax for specifying message strings to the Message Compiler<\/a> is to list each line of the message on a line by itself, with the message terminated by a line that begins with a period.<\/p>\n<pre>MessageId=0x1\r\nSeverity=Error\r\nFacility=Runtime\r\nSymbolicName=MSG_BAD_COMMAND\r\nLanguage=English\r\nYou have chosen an incorrect command.\r\n.\r\n<\/pre>\n<p>Since the way to specify the end of the message is to put a period at the start of a line, it means that the character that comes before the period is always a newline. (Well, except for the case where you have no lines at all.) The <code>%0<\/code> sequence is a workaround in the <code>Format\u00adMessage<\/code> function to let you stop processing the format string before it reaches that newline.<\/p>\n<p>There are other workarounds in the <code>Format\u00adMessage<\/code> function for limitations of the Message Compiler: You can use <code>%.<\/code> to generate a single period, if you want to start a message with a period. You can use <code>%b<\/code> to generate a single space. Check the documentation for the complete list.<\/p>\n<p>If you&#8217;re passing your own format string, then many of the special sequences in the format string aren&#8217;t of much use to you, because you could have applied those sequences to the string before passing it to the <code>Format\u00adMessage<\/code> function. The weird special sequences are primarily for the case where the strings came from the Message Compiler, and they give you a way to &#8220;pass through&#8221; special values that the Message Compiler syntax doesn&#8217;t allow for.<\/p>\n<p>\u00b9 Note, however, that the format string must still be a proper string with a null terminator. The <code>%0<\/code> sequence does not absolve you of the responsibility to pass a proper null-terminated string. It does tell the <code>Format\u00adMessage<\/code> to ignore the rest of the string, but there must still be a &#8220;rest of the string&#8221;.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Well, it&#8217;s not <CODE>Format&shy;Message<\/CODE> that&#8217;s adding the newline.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-103025","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Well, it&#8217;s not <CODE>Format&shy;Message<\/CODE> that&#8217;s adding the newline.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103025","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=103025"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/103025\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=103025"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=103025"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=103025"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}