Home | History | Annotate | Download | only in strings
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 #ifndef TENSORFLOW_LIB_STRINGS_NUMBERS_H_
     17 #define TENSORFLOW_LIB_STRINGS_NUMBERS_H_
     18 
     19 #include <string>
     20 
     21 #include "tensorflow/core/lib/core/stringpiece.h"
     22 #include "tensorflow/core/platform/types.h"
     23 
     24 namespace tensorflow {
     25 namespace strings {
     26 
     27 // ----------------------------------------------------------------------
     28 // FastIntToBufferLeft()
     29 //    These are intended for speed.
     30 //
     31 //    All functions take the output buffer as an arg.  FastInt() uses
     32 //    at most 22 bytes, FastTime() uses exactly 30 bytes.  They all
     33 //    return a pointer to the beginning of the output, which is the same as
     34 //    the beginning of the input buffer.
     35 //
     36 //    NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible
     37 //    to pass to FastTimeToBuffer() a time whose year cannot be
     38 //    represented in 4 digits. In this case, the output buffer
     39 //    will contain the string "Invalid:<value>"
     40 // ----------------------------------------------------------------------
     41 
     42 // Previously documented minimums -- the buffers provided must be at least this
     43 // long, though these numbers are subject to change:
     44 //     Int32, UInt32:                   12 bytes
     45 //     Int64, UInt64, Int, Uint:        22 bytes
     46 //     Time:                            30 bytes
     47 // Use kFastToBufferSize rather than hardcoding constants.
     48 static const int kFastToBufferSize = 32;
     49 
     50 // ----------------------------------------------------------------------
     51 // FastInt32ToBufferLeft()
     52 // FastUInt32ToBufferLeft()
     53 // FastInt64ToBufferLeft()
     54 // FastUInt64ToBufferLeft()
     55 //
     56 // These functions convert their numeric argument to an ASCII
     57 // representation of the numeric value in base 10, with the
     58 // representation being left-aligned in the buffer.  The caller is
     59 // responsible for ensuring that the buffer has enough space to hold
     60 // the output.  The buffer should typically be at least kFastToBufferSize
     61 // bytes.
     62 //
     63 // Returns a pointer to the end of the string (i.e. the null character
     64 // terminating the string).
     65 // ----------------------------------------------------------------------
     66 
     67 char* FastInt32ToBufferLeft(int32 i, char* buffer);    // at least 12 bytes
     68 char* FastUInt32ToBufferLeft(uint32 i, char* buffer);  // at least 12 bytes
     69 char* FastInt64ToBufferLeft(int64 i, char* buffer);    // at least 22 bytes
     70 char* FastUInt64ToBufferLeft(uint64 i, char* buffer);  // at least 22 bytes
     71 
     72 // Required buffer size for DoubleToBuffer is kFastToBufferSize.
     73 // Required buffer size for FloatToBuffer is kFastToBufferSize.
     74 char* DoubleToBuffer(double i, char* buffer);
     75 char* FloatToBuffer(float i, char* buffer);
     76 
     77 // Convert a 64-bit fingerprint value to an ASCII representation.
     78 string FpToString(Fprint fp);
     79 
     80 // Attempt to parse a fingerprint in the form encoded by FpToString.  If
     81 // successful, stores the fingerprint in *fp and returns true.  Otherwise,
     82 // returns false.
     83 bool StringToFp(const string& s, Fprint* fp);
     84 
     85 // Convert a 64-bit fingerprint value to an ASCII representation that
     86 // is terminated by a '\0'.
     87 // Buf must point to an array of at least kFastToBufferSize characters
     88 StringPiece Uint64ToHexString(uint64 v, char* buf);
     89 
     90 // Attempt to parse a uint64 in the form encoded by FastUint64ToHexString.  If
     91 // successful, stores the value in *v and returns true.  Otherwise,
     92 // returns false.
     93 bool HexStringToUint64(const StringPiece& s, uint64* v);
     94 
     95 // Convert strings to 32bit integer values.
     96 // Leading and trailing spaces are allowed.
     97 // Return false with overflow or invalid input.
     98 bool safe_strto32(StringPiece str, int32* value);
     99 
    100 // Convert strings to unsigned 32bit integer values.
    101 // Leading and trailing spaces are allowed.
    102 // Return false with overflow or invalid input.
    103 bool safe_strtou32(StringPiece str, uint32* value);
    104 
    105 // Convert strings to 64bit integer values.
    106 // Leading and trailing spaces are allowed.
    107 // Return false with overflow or invalid input.
    108 bool safe_strto64(StringPiece str, int64* value);
    109 
    110 // Convert strings to unsigned 64bit integer values.
    111 // Leading and trailing spaces are allowed.
    112 // Return false with overflow or invalid input.
    113 bool safe_strtou64(StringPiece str, uint64* value);
    114 
    115 // Convert strings to floating point values.
    116 // Leading and trailing spaces are allowed.
    117 // Values may be rounded on over- and underflow.
    118 bool safe_strtof(const char* str, float* value);
    119 
    120 // Convert strings to double precision floating point values.
    121 // Leading and trailing spaces are allowed.
    122 // Values may be rounded on over- and underflow.
    123 bool safe_strtod(const char* str, double* value);
    124 
    125 inline bool ProtoParseNumeric(StringPiece s, int32* value) {
    126   return safe_strto32(s, value);
    127 }
    128 
    129 inline bool ProtoParseNumeric(StringPiece s, uint32* value) {
    130   return safe_strtou32(s, value);
    131 }
    132 
    133 inline bool ProtoParseNumeric(StringPiece s, int64* value) {
    134   return safe_strto64(s, value);
    135 }
    136 
    137 inline bool ProtoParseNumeric(StringPiece s, uint64* value) {
    138   return safe_strtou64(s, value);
    139 }
    140 
    141 inline bool ProtoParseNumeric(StringPiece s, float* value) {
    142   return safe_strtof(s.ToString().c_str(), value);
    143 }
    144 
    145 inline bool ProtoParseNumeric(StringPiece s, double* value) {
    146   return safe_strtod(s.ToString().c_str(), value);
    147 }
    148 
    149 // Convert strings to number of type T.
    150 // Leading and trailing spaces are allowed.
    151 // Values may be rounded on over- and underflow.
    152 template <typename T>
    153 bool SafeStringToNumeric(StringPiece s, T* value) {
    154   return ProtoParseNumeric(s, value);
    155 }
    156 
    157 // Converts from an int64 to a human readable string representing the
    158 // same number, using decimal powers.  e.g. 1200000 -> "1.20M".
    159 string HumanReadableNum(int64 value);
    160 
    161 // Converts from an int64 representing a number of bytes to a
    162 // human readable string representing the same number.
    163 // e.g. 12345678 -> "11.77MiB".
    164 string HumanReadableNumBytes(int64 num_bytes);
    165 
    166 // Converts a time interval as double to a human readable
    167 // string. For example:
    168 //   0.001       -> "1 ms"
    169 //   10.0        -> "10 s"
    170 //   933120.0    -> "10.8 days"
    171 //   39420000.0  -> "1.25 years"
    172 //   -10         -> "-10 s"
    173 string HumanReadableElapsedTime(double seconds);
    174 
    175 }  // namespace strings
    176 }  // namespace tensorflow
    177 
    178 #endif  // TENSORFLOW_LIB_STRINGS_NUMBERS_H_
    179