Samples

Below are some samples of FormatC's output. These same samples are used for the unit tests, so check the tests for the corresponding code files.

Toggle light/dark

C# (with line numbers)

  1. using System;
  2. // A single line comment starting at the beginning of the line
  3. using System.IO;
  4. /* A comment block starting at the beginning of the line */
  5. 
  6. 
  7. namespace CSharpTest
  8. {
  9.     // A comment containing what looks like a "string"
 10.     // A comment containing /* what looks like */ a block comment
 11.     /* A multiline comment
 12.      * also containing a // regular comment
 13.      * And containing what looks like a "string"
 14.      */
 15. 
 16.     public class Program
 17.     {
 18.         /// <summary>
 19.         /// An XML comment <see cref="System.String" />.
 20.         /// </summary>
 21.         public static void Main()
 22.         {
 23.             int Int; // case sensitive test
 24.             int @int; // escaping test
 25.             int from; // contextual escaping test
 26.             Console.WriteLine("A string \" \\\" hello \t\\");
 27.             Console.WriteLine("A string containing what // looks like a comment."); // followed by a real comment
 28.             Console.WriteLine(@"An @ string"" \"); /* and another comment */
 29.             Console.WriteLine(@"Another @ string """"");
 30.             Console.WriteLine("{0} {1} {2}", "more than one\\", /* embedded comment */ @"string on ""the same", "line");
 31. #if PREPROCESSORTEST
 32.             for( int x = 0; x < 10; ++x )
 33.             {
 34.                 Console.WriteLine('x');
 35.                 Console.WriteLine('\'');
 36.                 Console.WriteLine('\\');
 37.             }
 38. #endif
 39.         }
 40. 
 41.         public async Task Foo()
 42.         {
 43.             await Bar();
 44.         }
 45.     }
 46. }

C++

#include <iostream>
#using <System.dll>
// A comment at the beginning of a line
#include "test.h"
/* A block comment at the beginning of a line */
using namespace std;

template<int x, int y>
struct gcd
{
    /* Another block comment
     * containg a // regular comment
     * and what looks like a "string"
     */
    static const int value = gcd<y, x % y>::value;
};

template<int x>
struct gcd<x, 0>
{
    // A comment containing /* a block comment */ and what looks like a "string"
    static const int value = x;
};

int main()
{
    int Int; // case sensitive test
    int x = gcd<511, 42>::value;
    cout << x << endl;
    cout << "It's a \"string\\\" \\" << endl; /* another block comment */
    cout << "A string /* containg what */ looks like // a comment." << endl; // and a real comment
    cout << 'c' << '\'' << '\\' << endl;
    cout << "more" << "than\"" << "one string on the same line" << endl;

    return 0;
}

PowerShell (using System.Management.Automation)

param(
  [parameter(Position=0, Mandatory=$true)][string]$VirtualMachineName,
  [parameter(Position=1, Mandatory=$true)][string]$FloppyPath,
  [parameter(Mandatory=$false)][Switch]$HostDrive
)

#asdfsadf

$vpc = New-Object -ComObject "VirtualPC.Application"
$vm = $vpc.FindVirtualMachine($VirtualMachineName)
if( $vm -eq $null )
{
    Write-Error "Virtual machine not found."
}
else
{
    if( $HostDrive )
    {
        $vm.FloppyDrives.Item(1).AttachHostDrive($FloppyPath)
    }
    else
    {
        $vm.FloppyDrives.Item(1).AttachImage($FloppyPath)
    }
}

XML

<?xml version="1.0"
      encoding="utf-8" ?>
<Foo xmlns:html="http://www.w3.org/1999/xhtml">
  <html:a href='http://www.ookii.org'> not="an attribute"
    <!-- A comment containing what looks like an <element> and a "string" -->
  </html:a>
  &lt;entities&gt;
  <Foo-Bar test="single ' quotes are valid in double quoted attributes=" test2='and double " inside single'
       test3="and &quot; valid anywhere"
       >
    <![CDATA[ A CDATA section 
      Containing what looks like an <element> and a "string"
    ]]>
  </Foo-Bar>
  <element.with.a.period.in.it attribute.with.period="test">
  </element.with.a.period.in.it>
</Foo>

Visual Basic

Imports System
' A comment at the beginning of a line
Imports System.IO
REM A REM comment at the beginning of a line
Imports <xmlns:a="http://www.ookii.org">

Namespace VisualBasicTest
    ' A comment, containing what looks like a "string"
    REM A REM comment containg an ' ordinary comment
    Public Class Program
        Public Sub Main()
            Dim [Integer] As Integer ' Escape test
            Dim From As String ' Contextual escape test

            Console.WriteLine("A string""hello""")
            for x = 0 to 10 ' Lowercase for case insensitive test.
                Console.WriteLine("a"c)
            Next
            Dim foo = <?xml version="1.0"?>
                      <Foo test=<%= Date.Now %> a:bar="baz">
                          <%= If(a, <a />, <b c=<%= Date.UtcNow %>/>) %>
                      </Foo>
            Dim bar = From f In foo _
                      Where f.<Foo>.@a:bar = "baz"
#If TEST Then
            Console.WriteLine("{0} {1} {2}", "more than one", "string on ""the same", "line");
#End If
        End Sub
    End Class
End Namespace

T-SQL

--a comment containg a "string"
/* and another comment
 * containing what looks like -- a regular comment
 * and a "string"
 */

SELECT * FROM Foo;

SELECT [from] FROM "Select";

INSERT INTO Bar(test, test1, test2)
VALUES ('a string', 'another'' string\', N'A widechar'' string');

select test from bar -- case insensitive test

SELECT 'a string /* containing */' + ' what -- looks like a comment' /* and a real comment */

SELECT @@VERSION;