Home | History | Annotate | Download | only in lite_strings
      1 /*
      2  * Copyright (C) 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STR_CAT_H_
     18 #define NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STR_CAT_H_
     19 
     20 // Less efficient but more compact versions of several absl string utils.
     21 //
     22 // "More compact" means "pulls in fewer code dependencies".  That's useful if
     23 // one tries to minimize the code size.
     24 //
     25 // Note: the name and the signature of the functions from this header were
     26 // chosen to minimize the effort of converting code that uses absl::LiteStrCat &
     27 // co to our more compact functions.
     28 
     29 #include <string>
     30 
     31 #ifdef COMPILER_MSVC
     32 #include <sstream>
     33 #endif  // COMPILER_MSVC
     34 
     35 namespace libtextclassifier3 {
     36 namespace mobile {
     37 
     38 // Less efficient but more compact version of absl::LiteStrCat().
     39 //
     40 // Given a value v (see supported types below) LiteStrCat(v) returns a new
     41 // string that contains the representation of v.  For examples, see
     42 // str-cat_test.cc.
     43 template <typename T>
     44 inline string LiteStrCat(T v) {
     45 #ifdef COMPILER_MSVC
     46   std::stringstream stream;
     47   stream << input;
     48   return stream.str();
     49 #else
     50   return std::to_string(v);
     51 #endif
     52 }
     53 
     54 template <>
     55 inline string LiteStrCat(const char *v) {
     56   return string(v);
     57 }
     58 
     59 // TODO(salcianu): use a reference type (const string &).  For some reason, I
     60 // couldn't get that to work on a first try.
     61 template <>
     62 inline string LiteStrCat(string v) {
     63   return v;
     64 }
     65 
     66 template <>
     67 inline string LiteStrCat(char v) {
     68   return string(1, v);
     69 }
     70 
     71 // Less efficient but more compact version of absl::LiteStrAppend().
     72 template <typename T>
     73 inline void LiteStrAppend(string *dest, T v) {
     74   dest->append(LiteStrCat(v));  // NOLINT
     75 }
     76 
     77 template <typename T1, typename T2>
     78 inline void LiteStrAppend(string *dest, T1 v1, T2 v2) {
     79   dest->append(LiteStrCat(v1));  // NOLINT
     80   dest->append(LiteStrCat(v2));  // NOLINT
     81 }
     82 
     83 template <typename T1, typename T2, typename T3>
     84 inline void LiteStrAppend(string *dest, T1 v1, T2 v2, T3 v3) {
     85   LiteStrAppend(dest, v1, v2);
     86   dest->append(LiteStrCat(v3));  // NOLINT
     87 }
     88 
     89 template <typename T1, typename T2, typename T3, typename T4>
     90 inline void LiteStrAppend(string *dest, T1 v1, T2 v2, T3 v3, T4 v4) {
     91   LiteStrAppend(dest, v1, v2, v3);
     92   dest->append(LiteStrCat(v4));  // NOLINT
     93 }
     94 
     95 }  // namespace mobile
     96 }  // namespace nlp_saft
     97 
     98 #endif  // NLP_SAFT_COMPONENTS_COMMON_MOBILE_LITE_STRINGS_STR_CAT_H_
     99