LCOV - code coverage report
Current view: top level - detail - characters.h (source / functions) Coverage Total Hit
Test: coverage.filtered.info Lines: 100.0 % 2 2
Test Date: 2026-02-09 12:52:35 Functions: 100.0 % 1 1
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /**
       2                 :             : * Copyright © 2025 Valentin Gorelov
       3                 :             : *
       4                 :             : * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
       5                 :             : * documentation files (the “Software”), to deal in the Software without restriction,
       6                 :             : * including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
       7                 :             : * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
       8                 :             : * subject to the following conditions:
       9                 :             : *
      10                 :             : * The above copyright notice and this permission notice
      11                 :             : * shall be included in all copies or substantial portions of the Software.
      12                 :             : *
      13                 :             : * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
      14                 :             : * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      15                 :             : * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
      16                 :             : * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      17                 :             : * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      18                 :             : */
      19                 :             : 
      20                 :             : /**
      21                 :             :  * @brief
      22                 :             :  * @author Valentin Gorelov <gorelov.valentin@gmail.com>
      23                 :             :  */
      24                 :             : 
      25                 :             : #ifndef ATCMD_CHARACTERS_H
      26                 :             : #define ATCMD_CHARACTERS_H
      27                 :             : 
      28                 :             : #include <cstdint>
      29                 :             : #include <array>
      30                 :             : #include <assert.h>
      31                 :             : 
      32                 :             : #include <atcmd/common.h>
      33                 :             : 
      34                 :             : namespace atcmd::detail {
      35                 :             : 
      36                 :             : struct Characters
      37                 :             : {
      38                 :             :         Characters() = delete;
      39                 :             : 
      40                 :             :         static constexpr bool isUpperAlphabetic(char ch)
      41                 :             :         {
      42                 :             :                 return (ch >= 'A') && (ch <= 'Z');
      43                 :             :         }
      44                 :             : 
      45                 :             :         static constexpr bool isLowerAlphabetic(char ch)
      46                 :             :         {
      47                 :             :                 return (ch >= 'a') && (ch <= 'z');
      48                 :             :         }
      49                 :             : 
      50                 :             :         static constexpr bool isAlphabetic(char ch)
      51                 :             :         {
      52                 :             :                 return isUpperAlphabetic(ch) || isLowerAlphabetic(ch);
      53                 :             :         }
      54                 :             : 
      55                 :             :         static constexpr bool isNumeric(char ch)
      56                 :             :         {
      57                 :             :                 return (ch >= '0') && (ch <= '9');;
      58                 :             :         }
      59                 :             : 
      60                 :             :         static constexpr int_fast8_t getNumeric(char ch)
      61                 :             :         {
      62                 :             :                 if (!isNumeric(ch))
      63                 :             :                 {
      64                 :             :                         return -1;
      65                 :             :                 }
      66                 :             :                 else
      67                 :             :                 {
      68                 :             :                         return ch - '0';
      69                 :             :                 }
      70                 :             :         }
      71                 :             : 
      72                 :             :         static constexpr int_fast8_t getHex(char ch)
      73                 :             :         {
      74                 :             :                 int_fast8_t r = getNumeric(ch);
      75                 :             :                 if (r == -1)
      76                 :             :                 {
      77                 :             :                         if ((ch >= 'a') && (ch <= 'f'))
      78                 :             :                         {
      79                 :             :                                 r = 10 + (ch - 'a');
      80                 :             :                         }
      81                 :             :                         else if ((ch >= 'A') && (ch <= 'F'))
      82                 :             :                         {
      83                 :             :                                 r = 10 + (ch - 'A');
      84                 :             :                         }
      85                 :             :                         else
      86                 :             :                         {
      87                 :             :                                 r = -1;
      88                 :             :                         }
      89                 :             :                 }
      90                 :             :                 return r;
      91                 :             :         }
      92                 :             : 
      93                 :             :         static char toUpper(char ch);
      94                 :             : 
      95                 :             :         static constexpr uint8_t encode(char ch)
      96                 :             :         {
      97                 :             :                 static constexpr uint8_t alphabetic_offset = 'Z' - 'A' + 1;
      98                 :             :                 static constexpr uint8_t numeric_offset = alphabetic_offset + ('9' - '0' + 1);
      99                 :             : 
     100                 :             :                 if (isUpperAlphabetic(ch))
     101                 :             :                 {
     102                 :             :                         return ch - 'A';
     103                 :             :                 }
     104                 :             :                 else if (isNumeric(ch))
     105                 :             :                 {
     106                 :             :                         return alphabetic_offset + (ch - '0');
     107                 :             :                 }
     108                 :             :                 else
     109                 :             :                 {
     110                 :             :                         switch (ch) {
     111                 :             :                         case '!':
     112                 :             :                                 return numeric_offset;
     113                 :             :                         case '%':
     114                 :             :                                 return numeric_offset + 1;
     115                 :             :                         case '-':
     116                 :             :                                 return numeric_offset + 2;
     117                 :             :                         case '.':
     118                 :             :                                 return numeric_offset + 3;
     119                 :             :                         case '/':
     120                 :             :                                 return numeric_offset + 4;
     121                 :             :                         case ':':
     122                 :             :                                 return numeric_offset + 5;
     123                 :             :                         case '_':
     124                 :             :                                 return numeric_offset + 6;
     125                 :             :                         default:
     126                 :             :                                 return 0xFF;
     127                 :             :                         }
     128                 :             :                 }
     129                 :             :         }
     130                 :             : 
     131                 :      764737 :         static constexpr char decode(uint8_t encoded)
     132                 :             :         {
     133                 :             :                 assert(encoded < getAlphabetSize());
     134                 :      764737 :                 return alphabet[encoded];
     135                 :             :         }
     136                 :             : 
     137                 :             :         static void printNumber(uint32_t number, uint8_t base, PrintCharCallback callback, void* context);
     138                 :             :         static void printHexadecimalString(const uint8_t* data, uint16_t size, PrintCharCallback callback, void* context);
     139                 :             : 
     140                 :             :         static constexpr uint_fast8_t getAlphabetSize()
     141                 :             :         {
     142                 :             :                 return std::size(alphabet);
     143                 :             :         }
     144                 :             : 
     145                 :             : private:
     146                 :             :         static inline constexpr char alphabet[] =
     147                 :             :         {
     148                 :             :                 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
     149                 :             :                 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
     150                 :             :                 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
     151                 :             :                 '!', '%', '-', '.', '/', ':', '_'
     152                 :             :         };
     153                 :             : };
     154                 :             : 
     155                 :             : }
     156                 :             : 
     157                 :             : #endif // ATCMD_CHARACTERS_H
        

Generated by: LCOV version 2.0-1