Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python-like format syntax #18

Open
WPMGPRoSToTeMa opened this issue Jun 20, 2018 · 2 comments
Open

Python-like format syntax #18

WPMGPRoSToTeMa opened this issue Jun 20, 2018 · 2 comments
Labels
enhancement Priority: Medium sourcepawn Related to SorucePawn extension

Comments

@WPMGPRoSToTeMa
Copy link

Examples:

Format("Hello, {:s}!", "world"); // "Hello, world"
Format("{0:s}{1:s}{0:s}", "abra", "cad"); // "abracadabra"
Format("The answer is {:d}.", 42); // "42"
Format("Hello, {name:s}! The answer is {number:d}. Goodbye, {name:s}.", FmtArg("name", "World"), FmtArg("number", 42)); // "Hello, World! The answer is 42. Goodbye, World."

There is tagof operator in SP, can we use it for type auto-examination? Can we use it at least for FmtArg? If not, should we add argument in FmtArg to pass the type?

Also we can choose default type for {}, but there are several frequently used types, so I don't prefer it.

@Amaroq7
Copy link
Owner

Amaroq7 commented Jun 21, 2018

This looks handy. I think we could we use fmtlib for this kind of formatting. Could you provide some example how would you like to use tagof operator?

@WPMGPRoSToTeMa
Copy link
Author

Something like this:

#define A(%0) FmtArg(#%0, tagof(%0), %0)

stock SomePackedType FmtArg(const char[] name, int tag, any ...) {
  int intVar;
  float floatVar;
  char charVar;
#pragma unused intVar
#pragma unused floatVar
#pragma unused charVar
  
  EnumOfTypes type;
  switch (tag) {
    case tagof(intVar): type = Type_Int;
    case tagof(floatVar): type = Type_Float;
    case tagof(charVar): type = Type_String;
  }
  
  return /*packed name, type and variable value*/;
}

Usage example:

Format("You hit {victimName} ({victimHealth} hp) with {damage} damage", A(victimName), A(victimHealth), A(damage));

Idk how to check if variable is an array. Unfortunately sizeof can't help here (it gives a compile-time error for unsized arrays).

What about fmtlib, idk how it can help us, because we can't just extract parsing code from it.

@Amaroq7 Amaroq7 added the sourcepawn Related to SorucePawn extension label Oct 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Priority: Medium sourcepawn Related to SorucePawn extension
Projects
None yet
Development

No branches or pull requests

2 participants