-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Printf formatters (String and Int types) (#19)
- Loading branch information
1 parent
31daf19
commit df1f026
Showing
6 changed files
with
178 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <VariableFormatters.h> | ||
#include <ArduinoJson.h> | ||
|
||
static const char FORMAT_ARG_NAME[] = "format"; | ||
|
||
PrintfFormatterNumeric::PrintfFormatterNumeric(const String& formatSchema) | ||
: formatSchema(formatSchema) | ||
{ } | ||
|
||
std::shared_ptr<const PrintfFormatterNumeric> PrintfFormatterNumeric::build(JsonObject args) { | ||
String formatSchema; | ||
|
||
if (args.containsKey(FORMAT_ARG_NAME)) { | ||
formatSchema = args[FORMAT_ARG_NAME].as<const char*>(); | ||
// In case someone does a schema that can crash the ESP | ||
formatSchema.replace("%s", "ERR"); | ||
// Replacing the double percent in case the user needs a percent sign in the output | ||
// Replacing it with a (hopefully) impossible character should make sure that it doesn't get un-replaced by anything that is needed. | ||
// \a is the bell/ding that can flash your console/cause it to make a sound. | ||
formatSchema.replace("%%", "\a"); | ||
// This makes sure that if they add more than one formatter, it will only use the first and only argument. | ||
formatSchema.replace("%", "%1$"); | ||
// Undoing the first replace so the escaped percent can be printed | ||
formatSchema.replace("\a", "%%"); | ||
} else { | ||
formatSchema = "%1$d"; | ||
} | ||
|
||
return std::shared_ptr<const PrintfFormatterNumeric>(new PrintfFormatterNumeric(formatSchema)); | ||
} | ||
|
||
String PrintfFormatterNumeric::format(const String &value) const { | ||
int numericValue = value.toInt(); | ||
|
||
char buffer[120]; | ||
snprintf(buffer, sizeof(buffer), formatSchema.c_str(), numericValue); | ||
|
||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <VariableFormatters.h> | ||
#include <ArduinoJson.h> | ||
|
||
static const char FORMAT_ARG_NAME[] = "format"; | ||
|
||
PrintfFormatterString::PrintfFormatterString(const String& formatSchema) | ||
: formatSchema(formatSchema) | ||
{ } | ||
|
||
std::shared_ptr<const PrintfFormatterString> PrintfFormatterString::build(JsonObject args) { | ||
String formatSchema; | ||
|
||
if (args.containsKey(FORMAT_ARG_NAME)) { | ||
formatSchema = args[FORMAT_ARG_NAME].as<const char*>(); | ||
// Replacing the double percent in case the user needs a percent sign in the output | ||
// Replacing it with a (hopefully) impossible character should make sure that it doesn't get un-replaced by anything that is needed. | ||
formatSchema.replace("%%", "\a"); | ||
// This makes sure that if they add more than one formatter, it will only use the first and only argument. | ||
formatSchema.replace("%", "%1$"); | ||
// Undoing the first replace so the escaped percent can be printed | ||
formatSchema.replace("\a", "%%"); | ||
} else { | ||
formatSchema = "%1$s"; | ||
} | ||
|
||
return std::shared_ptr<const PrintfFormatterString>(new PrintfFormatterString(formatSchema)); | ||
} | ||
|
||
String PrintfFormatterString::format(const String &value) const { | ||
char buffer[120]; | ||
snprintf(buffer, sizeof(buffer), formatSchema.c_str(), value.c_str()); | ||
|
||
return buffer; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters