Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
GyverLibs committed Jun 5, 2022
1 parent dc2d98e commit 23317a7
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 4 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Foo](https://img.shields.io/badge/Version-1.3.1-brightgreen.svg?style=flat-square)](#versions)
[![Foo](https://img.shields.io/badge/Version-1.4-brightgreen.svg?style=flat-square)](#versions)
[![Foo](https://img.shields.io/badge/Website-AlexGyver.ru-blue.svg?style=flat-square)](https://alexgyver.ru/)
[![Foo](https://img.shields.io/badge/%E2%82%BD$%E2%82%AC%20%D0%9D%D0%B0%20%D0%BF%D0%B8%D0%B2%D0%BE-%D1%81%20%D1%80%D1%8B%D0%B1%D0%BA%D0%BE%D0%B9-orange.svg?style=flat-square)](https://alexgyver.ru/support_alex/)

Expand Down Expand Up @@ -95,6 +95,25 @@ uint8_t GP_listToNum(char* list, int* data, uint8_t size, char div = ',');
uint32_t GP_decodeColor(char* hex);
```
### Unicode
См. пример **testUnicode**
```cpp
char* GP_uniencode(int32_t c, char* s); // код unicode в char[5]
String GP_uniencode(uint16_t c); // код unicode в String
uint16_t GP_unistrlen(char* data); // длина unicode строки в кол-ве символов
```

### Url encode/decode
См. пример **testUrl**

```cpp
void GP_urlencode(const String& s, String& dest); // urlencode из String в String
String GP_urlencode(const String& s); // urlencode из String в String (возврат)
String GP_urldecode(const String& s, String& dest); // urldecode из String в String
String GP_urldecode(const String& s); // urldecode из String в String (возврат)
```
<a id="example"></a>
## Пример
Остальные примеры смотри в **examples**!
Expand Down Expand Up @@ -161,6 +180,7 @@ void loop() {
- v1.2 - добавлены ещё инструменты для парсинга
- v1.3 - добавлена возможность восстановить строку
- v1.3.1 - фикс warning
- v1.4 - добавил url и unicode кодирование

<a id="feedback"></a>
## Баги и обратная связь
Expand Down
15 changes: 15 additions & 0 deletions examples/testUnicode/testUnicode.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// тест утилит для парсинга
#include <GParser.h>

void setup() {
Serial.begin(9600);
// неправильно посчитает длину строки
Serial.println(strlen("Привет!"));
// правильно посчитает длину строки
Serial.println(GP_unistrlen("Привет!"));

Serial.println(GP_uniencode(0x27A1));
}

void loop() {
}
12 changes: 12 additions & 0 deletions examples/testUrl/testUrl.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// тест утилит для парсинга
#include <GParser.h>

void setup() {
Serial.begin(9600);
String enc = GP_urlencode("https://kek.ru/#абвг");
Serial.println(enc);
Serial.println(GP_urldecode(enc));
}

void loop() {
}
5 changes: 5 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ GP_numFromList KEYWORD2
GP_listToNum KEYWORD2
GP_decodeColor KEYWORD2

GP_urlencode KEYWORD2
GP_urldecode KEYWORD2
GP_uniencode KEYWORD2
GP_unistrlen KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################
6 changes: 3 additions & 3 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=GParser
version=1.3.1
version=1.4
author=AlexGyver <alex@alexgyver.ru>
maintainer=AlexGyver <alex@alexgyver.ru>
sentence=Fast library for parsing cstring into substrings
paragraph=Fast library for parsing cstring into substrings
sentence=Fast library for parsing cstring into substrings, urlencode, unicode encode for Arduino
paragraph=Fast library for parsing cstring into substrings, urlencode, unicode encode for Arduino
category=Data Processing
url=https://github.com/GyverLibs/GParser
architectures=*
3 changes: 3 additions & 0 deletions src/GParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
v1.2 - добавлены ещё инструменты для парсинга
v1.3 - добавлена возможность восстановить строку
v1.3.1 - фикс warning
v1.4 - добавил url и unicode кодирование
*/

#ifndef _GParser_h
#define _GParser_h
#include "parseUtils.h"
#include "unicode.h"
#include "url.h"

class GParser {
public:
Expand Down
37 changes: 37 additions & 0 deletions src/unicode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "unicode.h"
char* GP_uniencode(int32_t c, char* s) {
if (c < 0x80) {
s[0] = (c & 0x7F) | 0x00;
s[1] = 0;
} else if (c < 0x0800) {
s[0] = (c >> 6 & 0x1F) | 0xC0;
s[1] = (c & 0x3F) | 0x80;
s[2] = 0;
} else if (c < 0x010000) {
s[0] = (c >> 12 & 0x0F) | 0xE0;
s[1] = (c >> 6 & 0x3F) | 0x80;
s[2] = (c & 0x3F) | 0x80;
s[3] = 0;
} else if (c < 0x110000) {
s[0] = (c >> 18 & 0x07) | 0xF0;
s[1] = (c >> 12 & 0x3F) | 0x80;
s[2] = (c >> 6 & 0x3F) | 0x80;
s[3] = (c & 0x3F) | 0x80;
s[4] = 0;
}
return s;
}

String GP_uniencode(uint16_t c) {
char str[5];
return String(GP_uniencode(c, str));
}

uint16_t GP_unistrlen(char* data) {
uint16_t i = 0, count = 0;
while (data[i]) {
if ((data[i] & 0xc0) != 0x80) count++;
i++;
}
return count;
}
5 changes: 5 additions & 0 deletions src/unicode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once
#include <Arduino.h>
char* GP_uniencode(int32_t c, char* s);
String GP_uniencode(uint16_t c);
uint16_t GP_unistrlen(char* data);
48 changes: 48 additions & 0 deletions src/url.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "url.h"
void GP_urlencode(const String& s, String& dest) {
dest.reserve(s.length());
char c;
for (uint16_t i = 0; i < s.length(); i++) {
c = s[i];
if (c == ' ') dest += '+';
else if (!(
(c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '-' || c == '_' || c == '.' || c == '!' || c == '~' ||
c == '*' || c == '\'' || c == '(' || c == ')'
)) {
dest += '%';
dest += (char)((c >> 4) + (((c >> 4) > 9) ? 55 : 48));
dest += (char)((c & 0xF) + (((c & 0xF) > 9) ? 55 : 48));
} else dest += c;
}
}

String GP_urlencode(const String& s) {
String dest;
GP_urlencode(s, dest);
return dest;
}

void GP_urldecode(const String& s, String& dest) {
dest.reserve(s.length());
char c;
for (uint16_t i = 0; i < s.length(); i++) {
c = s[i];
if (c != '%') dest += (c == '+') ? ' ' : c;
else {
c = s[++i];
uint8_t v1 = c - ((c <= '9') ? 48 : ((c <= 'F') ? 55 : 87));
c = s[++i];
uint8_t v2 = c - ((c <= '9') ? 48 : ((c <= 'F') ? 55 : 87));
dest += char(v2 | (v1 << 4));
}
}
}

String GP_urldecode(const String& s) {
String dest;
GP_urldecode(s, dest);
return dest;
}
6 changes: 6 additions & 0 deletions src/url.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#pragma once
#include <Arduino.h>
void GP_urlencode(const String& s, String& dest);
String GP_urlencode(const String& s);
void GP_urldecode(const String& s, String& dest);
String GP_urldecode(const String& s);

0 comments on commit 23317a7

Please sign in to comment.