github.com/remobjects/goldbaselibrary@v0.0.0-20230924164425-d458680a936b/Source/Gold/fmt.pas (about)

     1  namespace go.fmt;
     2  
     3  uses go.fmt {$IFDEF CLR}, System.Linq {$ENDIF};
     4  
     5  method Errorf(fmt: String; params x: array of Object): builtin.error; public;
     6  begin
     7    exit errors.New(Sprintf(fmt, x));
     8  end;
     9  
    10  method Fprintf(w: io.Writer; fmt: String; params x: array of Object): tuple of (Integer, builtin.error); public;
    11  begin
    12    {$IFDEF CLR}
    13    exit w.Write(new builtin.Slice<Byte>(System.Text.Encoding.UTF8.GetBytes(Sprintf(fmt, x))));
    14    {$ELSE}
    15    exit w.Write(new builtin.Slice<Byte>(Encoding.UTF8.GetBytes(Sprintf(fmt, x))));
    16    {$ENDIF}
    17  end;
    18  
    19  method Fprintln(w: io.Writer;  params x: array of Object): tuple of (Integer, builtin.error); public;
    20  begin
    21    {$IFDEF CLR}
    22    exit w.Write(new builtin.Slice<Byte>(System.Text.Encoding.UTF8.GetBytes(Sprint(x) + Environment.NewLine)));
    23    {$ELSE}
    24    exit w.Write(new builtin.Slice<Byte>(Encoding.UTF8.GetBytes(Sprint(x)+ Environment.NewLine)));
    25    {$ENDIF}
    26  end;
    27  
    28  method Fprint(w: io.Writer;  params x: array of Object): tuple of (Integer, builtin.error); public;
    29  begin
    30    {$IFDEF CLR}
    31    exit w.Write(new builtin.Slice<Byte>(System.Text.Encoding.UTF8.GetBytes(Sprint(x))));
    32    {$ELSE}
    33    exit w.Write(new builtin.Slice<Byte>(Encoding.UTF8.GetBytes(Sprint(x))));
    34    {$ENDIF}
    35  end;
    36  
    37  method Sprintln(params x: array of Object): String; public;
    38  begin
    39    exit Sprint(x)+Environment.NewLine;
    40  end;
    41  
    42  method Sprintf(fmt: String; params x: array of Object): String; public;
    43  begin
    44    raise new NotImplementedException();
    45  end;
    46  
    47  method Sprint(params x: array of Object): String; public;
    48  begin
    49    if length(x) = 0 then exit '';
    50    var s := coalesce(x[0]:ToString, '');
    51    var lastwasstring := x[0] is String;
    52    for i: Integer := 1 to length(x) -1 do begin
    53      var val := coalesce(x[i]:ToString, '');
    54      if not (x[i] is String) and not (lastwasstring) then
    55        s := s + ' ' + val
    56      else
    57        s := s + val;
    58      lastwasstring := s[i] is String;
    59    end;
    60    exit s;
    61  end;
    62  
    63  method Println(params x: array of Object); public;
    64  begin
    65    writeLn(Sprint(x));
    66  end;
    67  
    68  method Print(params x: array of Object); public;
    69  begin
    70    write(Sprint(x));
    71  end;
    72  
    73  method Printf(s: String; params x: array of Object); public;
    74  begin
    75    writeLn(Sprintf(s, x));
    76  end;
    77  
    78  
    79  method Sscan(str: String; params args: array of Object): tuple of (Integer, builtin.error);
    80  begin
    81    var v := str.Split([#9, #32], StringSplitOptions.RemoveEmptyEntries);
    82    for i: Integer := 0 to Math.Min(args.Length, v.Length) -1 do begin
    83      if args[i] is builtin.IReference then
    84        builtin.IReference(args[i]).Set(v[i])
    85      else
    86        args[i] := v[i];
    87    end;
    88  
    89    if v.Length > args.Length then begin
    90      exit (args.Length, Errors.new('Got '+v.Length+' items'));
    91    end;
    92    exit (v.Length, nil);
    93  end;
    94  
    95  method Sscanf(str, fmt: String; params args: array of Object): tuple of (Integer, builtin.error);
    96  begin
    97    exit Sscan(str, args);
    98  end;
    99  
   100  method Sscanln(str: String; params args: array of Object): tuple of (Integer, builtin.error);
   101  begin
   102    exit Sscan(str.TrimEnd([#13, #10]), args);
   103  end;
   104  
   105  type
   106    Stringer = public interface
   107      method String(): builtin.string;
   108    end;
   109    GoStringer = public interface
   110      method GoString(): builtin.string;
   111    end;
   112  end.