Jump to content
VoSs2o0o

New and easy Format Command (OpenSource)

Recommended Posts

I have created a unit, that is similar to String Interpolation. Of course real Interpolation would be a compiler feature.

 

TStr('Hello, {0}! Today is {1:ddd}, it'{1:HH:mm} now.').Params(name, date);

// Hello, Mark! Today is Wednesday, it's 19:40 now.

Variables where automaticly detected and converted (automatic type inferrence via Generics), so no need for IntToStr, FloatToStrF...

 

Here a short Description of the Format. It is similar to the Format Specifier of .Net. The Date Format is almost the same like in Delphi, but the other Specifier are different. but i think its a way better than the sprintf Convention that Delphi uses:

Integer and Float where automaticly converted and formatted as needed:
C for Currency (Number for Decimal Places)
D for Integer (Number for prefixed Zeros)
F for Double (Number for Decimal Places)

Date can formatted with:
dd = Day, MM=Month, yyyy=Year
HH=Hour, mm=Minute, ss=Seconds
there are further possibilities in the readme

Readme: https://github.com/VoSs2o0o/NetFormat/blob/master/readme.md

Code: https://github.com/VoSs2o0o/NetFormat/

Download: https://github.com/VoSs2o0o/NetFormat/releases

Website in German with descriptive Content about the Code: https://www.cloud-9.de/entwicklung/delphi-besserer-format-befehl

 

Edited by VoSs2o0o
  • Like 5
  • Thanks 4

Share this post


Link to post

Nice! I love seeing people think outside the box. I use string interpolation a lot in c# and I really dislike using Format in delphi.

 

Some suggestions

 

1) Unit Tests - using DUnitX - not just a test application. 
2) Prefix the Netformat.pas name with a namespace, eg VoSs2o0o.NetFormat.pas - this will avoid the chance that the unit name conflicts with other libraries ( the unit name is a bit generic).

3) TStr is not a great name for this, perhaps TFormatStr or TStringFormat or something like that would be more descriptive. 
4) Restructure the repo to be more organised - see https://www.finalbuilder.com/resources/blogs/advice-for-delphi-library-authors
5) Hex format would be useful. 

 

Anyway, Kudos on a great idea. 

Edited by Vincent Parrett
addition
  • Like 2

Share this post


Link to post
37 minutes ago, Vincent Parrett said:

3) TStr is not a great name for this, perhaps TFormatStr or TStringFormat or something like that would be more descriptive.

And maybe get rid of the leading T since it isn't really used as a type.

  • Like 2

Share this post


Link to post

Ok, that was valueable Input:

 

@Vincent:

1) done

2) good Idea, it has VoTools as Prefix now

3) The Idea is, that it is short like an f"test" in Python, $"test" in CSharp or a STR."test" in Java, but i have made two changes:

    - have changed it from 'TStr' to  'Fstr'

    - have readded the TNetFormat.Str format of the older Version, for a more Delphi like syntax

4) there are a lot of files now, so it was a good idea to restructure it

5) added 🙂

 

@Anders

See 3) above

 

@All:

I have a problem with the Generic<T> - Types, i cannot correctly detect 'nil':

I have tried the following:

default(T) == nil //cannot compile it
T(nil) == nil //cannot compile it
PPointer(@val1)^ <> nil // works, but not in DunitX

//i use this at moment (no direct Pointer supported at moment):
GetTypekind(T) == tkPointer //tkpointer is the type of nil, but alos of Pointers of course...

 

ChangeLog:

 

### 28.01.2024: Version 3.0
- Old:

TStr('Hallo {0} {1:D4} {2} {3}').
    Params('Welt', 55, 7.7, testdate);

- New: TStr -> FStr, and old Format readded, beause some People wants a more
  Delphi-Format like command:

FStr('Hallo {0} {1:D4} {2} {3}').
               Params('Welt', 55, 7.7, testdate);
TNetFormat.TStr('Hallo {0} {1:D4} {2} {3}',
               'Welt', 55, 7.7, testdate);

- Namespace VTools added to avoid conficts
- Hex-Format added ("X")
- TFormatStettings Support
- Tests added
- 'writeln' to Example added, to also have an console output
- some minors changes to README

 

Readme: https://github.com/VoSs2o0o/NetFormat/blob/master/README.md

Code: https://github.com/VoSs2o0o/NetFormat/

Download: https://github.com/VoSs2o0o/NetFormat/releases

Website in German with descriptive Content about the Code: https://www.cloud-9.de/entwicklung/delphi-besserer-format-befehl

Edited by VoSs2o0o
  • Like 3

Share this post


Link to post

Thanks for taking the feedback on board. Some comments

 

1) Move the Readme to the root folder of the repo, that way it shows on github when you vist the repo.

2) Change the Netformat to Source or Src
3) Move the .groupproj to the Source folder.

 

This is probably personal taste, but FStr might be confusing in Delphi code - especially for those coming to a code base and not being familiar with this library - since F is typically used to prefix class Fields. I understand why making it short is attractive.

 

 

  • Like 2

Share this post


Link to post
3 hours ago, Anders Melander said:

Laziness.

Yep, I'm too lazy to type all those 1's and 0's - life's too short for that!

  • Haha 2

Share this post


Link to post

Why not using the longer descriptive name for the declaration and then add a shorter type alias later. That way everyone can choose, while the more descriptive name is used by the IDE tooling.

 

  • Like 4

Share this post


Link to post

@Vincent: Of course the readme, license have to be in the root folder....

 

@Kas Ob., Uwe:

There are two Variants:

 

Lazy Coder:

FStr

and Delphi-Style

TNetFormat

and it is possible to use an alias for other variants.

by the way, i have released a Bugfix 3.0.1 for older Delphi Versions.

Share this post


Link to post

Thank you!

 

Just one think to explain and make it clear 

8 hours ago, VoSs2o0o said:

@Kas Ob., Uwe:

There are two Variants:

 

Lazy Coder:


FStr

and Delphi-Style


TNetFormat

This is not exactly about laziness, but also about how smooth it goes in slow brain like mine:

TNetFormat instantly invokes Network, Internet, protocol standard...

FStr instantly invokes a local field in Self or even local variable...

 

Anyway, thank you again and it is of course up to you to name it, for me i would not use it like that with these names, i will rename a type from it, something with clear path/history in my brain then use it, something shiny and unique 🤓

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×