firmware-base/docs-c/core/StringUtils.md
2025-06-04 16:50:34 +02:00

89 lines
3.1 KiB
Markdown

---
title: "StringUtils - String Manipulation Utilities"
description: "A utility class for string manipulation and type conversion for ESP-32 applications"
keywords: "ESP-32, C++17, string utilities, type conversion, industrial application, Modbus"
---
## StringUtils
**Path**: [`src/modbus/StringUtils.h`](../src/modbus/StringUtils.h)
**Revision History**: Initial documentation
StringUtils provides a set of utility functions for string manipulation and type conversion in an industrial ESP-32 application. It enables parsing and converting string values to native C types, which is particularly useful for configuration and communication scenarios in Modbus applications.
## REQUIREMENTS
No specific hardware pins are required as this is a software utility helper.
## PROVIDES
- `E_VALUE_TYPE` enumeration for representing different value types
- Template function `convertTo<T>` for converting strings to various native types
- Type detection functions (`isInteger`, `isFloat`, `detectType`)
- Hex data printing utility
## FEATURES
- Specialized template implementations for converting strings to:
- Integer types (`int`, `short`, `long int`)
- Floating-point (`float`)
- Boolean values (`bool`)
- Type detection to determine if a string represents a valid integer or floating-point value
- Platform-specific adaptations for string tokenization across different compilers
- Hex data visualization for debugging
## DEPENDENCIES
- [`./xtypes.h`](../src/modbus/xtypes.h)
- Standard C libraries: `<stdio.h>`, `<string.h>`, `<ctype.h>`, `<stdlib.h>`
```mermaid
graph TD
StringUtils --> xtypes
StringUtils --> "Standard C Libraries"
```
## BEHAVIOUR
The StringUtils component follows a simple conversion and detection workflow:
```mermaid
graph LR
Input[Input String] --> Detect[Detect Type]
Detect --> |Integer| ConvertInt[Convert to Integer]
Detect --> |Float| ConvertFloat[Convert to Float]
Detect --> |Boolean| ConvertBool[Convert to Boolean]
Detect --> |Text| HandleText[Handle as String]
ConvertInt --> Result[Native Type Result]
ConvertFloat --> Result
ConvertBool --> Result
HandleText --> Result
```
## TODOS
### PERFORMANCE
- Consider using `strtol`, `strtof`, etc. for all conversions to provide better error handling
- Optimize the string scanning operations for large strings
- Add string length validation to avoid buffer overruns
### SECURITY
- Add input validation to prevent exploitation through malformed strings
- Consider adding bounds checking for numeric conversions
- Implement sanitization for strings that will be used in sensitive contexts
### COMPLIANCE
- Ensure compatibility with C17 standard
- Verify that all functions work correctly in the target ESP-32 environment
- Audit for potential compliance issues with industrial standards
### RECOMMENDATIONS
- Extend type support to include additional numeric types as needed
- Add more robust error handling for conversion failures
- Consider adding string formatting utilities for consistent output generation
- For improved performance, use static or cached conversions for frequently used values