tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

strtools_public.h (6415B)


      1 //========= Copyright Valve Corporation ============//
      2 #pragma once
      3 
      4 #include <string>
      5 #include <stdint.h>
      6 #include <sys/types.h>
      7 #include <vector>
      8 
      9 /** returns true if the string has the prefix */
     10 bool StringHasPrefix( const std::string & sString, const std::string & sPrefix );
     11 bool StringHasPrefixCaseSensitive( const std::string & sString, const std::string & sPrefix );
     12 
     13 /** returns if the string has the suffix */
     14 bool StringHasSuffix( const std::string &sString, const std::string &sSuffix );
     15 bool StringHasSuffixCaseSensitive( const std::string &sString, const std::string &sSuffix );
     16 
     17 // Mozilla: see mozilla.patch for more details
     18 #if defined( _WIN32 )
     19 /** converts a UTF-16 string to a UTF-8 string */
     20 std::string UTF16to8( const wchar_t * in );
     21 std::string UTF16to8( const std::wstring & in );
     22 
     23 /** converts a UTF-8 string to a UTF-16 string */
     24 std::wstring UTF8to16(const char * in);
     25 std::wstring UTF8to16( const std::string & in );
     26 #define Utf16FromUtf8 UTF8to16
     27 #endif
     28 
     29 #if defined( _WIN32 )
     30 std::string DefaultACPtoUTF8( const char *pszStr );
     31 #endif
     32 
     33 /** Repairs a should-be-UTF-8 string to a for-sure-is-UTF-8 string, plus return boolean if we subbed in '?' somewhere */
     34 bool RepairUTF8( const char *begin, const char *end, std::string & sOutputUtf8 );
     35 bool RepairUTF8( const std::string & sInputUtf8, std::string & sOutputUtf8 );
     36 
     37 /** safely copy a string into a buffer */
     38 void strcpy_safe( char *pchBuffer, size_t unBufferSizeBytes, const char *pchSource );
     39 template< size_t bufferSize >
     40 void strcpy_safe( char (& buffer) [ bufferSize ], const char *pchSource ) 
     41 {
     42 strcpy_safe( buffer, bufferSize, pchSource );
     43 }
     44 
     45 
     46 /** converts a string to upper case */
     47 std::string StringToUpper( const std::string & sString );
     48 
     49 /** converts a string to lower case */
     50 std::string StringToLower( const std::string & sString );
     51 
     52 // we stricmp (from WIN) but it isn't POSIX - OSX/LINUX have strcasecmp so just inline bridge to it
     53 #if defined( OSX ) || defined( LINUX )
     54 #include <strings.h>
     55 inline int stricmp(const char *pStr1, const char *pStr2) { return strcasecmp(pStr1,pStr2); }
     56 #ifndef _stricmp
     57 #define _stricmp stricmp
     58 #endif
     59 inline int strnicmp( const char *pStr1, const char *pStr2, size_t unBufferLen ) { return strncasecmp( pStr1,pStr2, unBufferLen ); }
     60 #ifndef _strnicmp
     61 #define _strnicmp strnicmp
     62 #endif
     63 
     64 #ifndef _vsnprintf_s
     65 #define _vsnprintf_s vsnprintf
     66 #endif
     67 
     68 #define _TRUNCATE ((size_t)-1)
     69 
     70 #endif
     71 
     72 #if defined( OSX )
     73 // behaviors ensure NULL-termination at least as well as _TRUNCATE does, but
     74 // wcsncpy_s/strncpy_s can non-NULL-terminate, wcslcpy/strlcpy can not.
     75 // inline errno_t wcsncpy_s(wchar_t *strDest, size_t numberOfElements, const wchar_t *strSource, size_t count)
     76 // {
     77 // 	return wcslcpy(strDest, strSource, numberOfElements);
     78 // }
     79 
     80 // inline errno_t strncpy_s(char *strDest, size_t numberOfElements, const char *strSource, size_t count)
     81 // {
     82 // 	return strlcpy(strDest, strSource, numberOfElements);
     83 // }
     84 
     85 #endif
     86 
     87 #if defined( LINUX )
     88 // this implementation does not return whether or not the destination was 
     89 // truncated, but that is straightforward to fix if anybody actually needs the
     90 // return code. 
     91 #include "string.h"
     92 inline void wcsncpy_s(wchar_t *strDest, size_t numberOfElements, const wchar_t *strSource, size_t count)
     93 {
     94 wcsncpy(strDest, strSource, numberOfElements);
     95 strDest[numberOfElements-1] = '\0';
     96 }
     97 
     98 inline void strncpy_s(char *strDest, size_t numberOfElements, const char *strSource, size_t count)
     99 {
    100 strncpy(strDest, strSource, numberOfElements);
    101 strDest[numberOfElements-1] = '\0';
    102 }
    103 
    104 #endif
    105 
    106 #if defined( _WIN32 ) && _MSC_VER < 1800
    107 inline uint64_t strtoull(const char *str, char **endptr, int base) { return _strtoui64( str, endptr, base ); }
    108 #endif
    109 
    110 /* Handles copying a std::string into a buffer as would be provided in an API */
    111 uint32_t ReturnStdString( const std::string & sValue, char *pchBuffer, uint32_t unBufferLen );
    112 
    113 /** Returns a std::string from a uint64_t */
    114 // Mozilla: see mozilla.patch for more details
    115 //std::string Uint64ToString( uint64_t ulValue );
    116 
    117 /** returns a uint64_t from a string */
    118 uint64_t StringToUint64( const std::string & sValue );
    119 
    120 //-----------------------------------------------------------------------------
    121 // Purpose: Encodes a string (or binary data) from URL encoding format, see rfc1738 section 2.2.  
    122 //          This version of the call isn't a strict RFC implementation, but uses + for space as is
    123 //          the standard in HTML form encoding, despite it not being part of the RFC.
    124 //
    125 //          Dest buffer should be at least as large as source buffer to guarantee room for decode.
    126 //-----------------------------------------------------------------------------
    127 void V_URLEncode( char *pchDest, int nDestLen, const char *pchSource, int nSourceLen );
    128 
    129 /** Same as V_URLEncode, but without plus for space. */
    130 void V_URLEncodeNoPlusForSpace( char *pchDest, int nDestLen, const char *pchSource, int nSourceLen );
    131 
    132 /** Same as V_URLEncodeNoPlusForSpace, but without escaping / and : */
    133 void V_URLEncodeFullPath( char *pchDest, int nDestLen, const char *pchSource, int nSourceLen );
    134 
    135 
    136 //-----------------------------------------------------------------------------
    137 // Purpose: Decodes a string (or binary data) from URL encoding format, see rfc1738 section 2.2.  
    138 //          This version of the call isn't a strict RFC implementation, but uses + for space as is
    139 //          the standard in HTML form encoding, despite it not being part of the RFC.
    140 //
    141 //          Dest buffer should be at least as large as source buffer to guarantee room for decode.
    142 //			Dest buffer being the same as the source buffer (decode in-place) is explicitly allowed.
    143 //-----------------------------------------------------------------------------
    144 size_t V_URLDecode( char *pchDecodeDest, int nDecodeDestLen, const char *pchEncodedSource, int nEncodedSourceLen );
    145 
    146 /** Same as V_URLDecode, but without plus for space. */
    147 size_t V_URLDecodeNoPlusForSpace( char *pchDecodeDest, int nDecodeDestLen, const char *pchEncodedSource, int nEncodedSourceLen );
    148 
    149 //-----------------------------------------------------------------------------
    150 // Purpose: strip extension from a path
    151 //-----------------------------------------------------------------------------
    152 void V_StripExtension( std::string &in );
    153 
    154 
    155 /** Tokenizes a string into a vector of strings */
    156 std::vector<std::string> TokenizeString( const std::string & sString, char cToken );