]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/hex.h
Add sound to press event of some formspecs elements (#10402)
[minetest.git] / src / util / hex.h
index 6f00a79bff0a451115f4e1230fc1aa430d71203c..708f330242fb4a21b40d6b19ec51b76ad6b91979 100644 (file)
@@ -17,8 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef HEX_HEADER
-#define HEX_HEADER
+#pragma once
 
 #include <string>
 
@@ -27,12 +26,13 @@ static const char hex_chars[] = "0123456789abcdef";
 static inline std::string hex_encode(const char *data, unsigned int data_size)
 {
        std::string ret;
+       ret.reserve(data_size * 2);
+
        char buf2[3];
        buf2[2] = '\0';
 
-       for(unsigned int i = 0; i < data_size; i++)
-       {
-               unsigned char c = (unsigned char) data[i];
+       for (unsigned int i = 0; i < data_size; i++) {
+               unsigned char c = (unsigned char)data[i];
                buf2[0] = hex_chars[(c & 0xf0) >> 4];
                buf2[1] = hex_chars[c & 0x0f];
                ret.append(buf2);
@@ -43,20 +43,18 @@ static inline std::string hex_encode(const char *data, unsigned int data_size)
 
 static inline std::string hex_encode(const std::string &data)
 {
-    return hex_encode(data.c_str(), data.size());
+       return hex_encode(data.c_str(), data.size());
 }
 
 static inline bool hex_digit_decode(char hexdigit, unsigned char &value)
 {
-       if(hexdigit >= '0' && hexdigit <= '9')
+       if (hexdigit >= '0' && hexdigit <= '9')
                value = hexdigit - '0';
-       else if(hexdigit >= 'A' && hexdigit <= 'F')
+       else if (hexdigit >= 'A' && hexdigit <= 'F')
                value = hexdigit - 'A' + 10;
-       else if(hexdigit >= 'a' && hexdigit <= 'f')
+       else if (hexdigit >= 'a' && hexdigit <= 'f')
                value = hexdigit - 'a' + 10;
        else
                return false;
        return true;
 }
-
-#endif