• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #include "tensorflow/c/experimental/ops/gen/common/case_format.h"
16 
17 namespace tensorflow {
18 namespace generator {
19 
20 namespace {
21 
22 enum CaseFormatType {
23   LOWER_CAMEL,
24   UPPER_CAMEL,
25   LOWER_SNAKE,
26   UPPER_SNAKE,
27 };
28 
FormatStringCase(const string & str,CaseFormatType to,const char delimiter='_')29 string FormatStringCase(const string &str, CaseFormatType to,
30                         const char delimiter = '_') {
31   const bool from_snake =
32       (str == str_util::Uppercase(str)) || (str == str_util::Lowercase(str));
33   const bool toUpper = (to == UPPER_CAMEL || to == UPPER_SNAKE);
34   const bool toSnake = (to == LOWER_SNAKE || to == UPPER_SNAKE);
35 
36   string result;
37 
38   bool inputStart = true;
39   bool wordStart = true;
40   for (const char c : str) {
41     // Find a word start.
42     if (c == delimiter) {
43       // Repeated cases of wordStart means explicit delimiter usage.
44       if (wordStart) {
45         result.push_back(delimiter);
46       }
47       wordStart = true;
48       continue;
49     }
50     if (!from_snake && isupper(c)) {
51       wordStart = true;
52     }
53 
54     // add delimiter
55     if (wordStart && toSnake && !inputStart) {
56       result.push_back(delimiter);
57     }
58 
59     // add the next letter from the input string (choosing upper/lower case)
60     const bool shouldCapIfSnake = toUpper;
61     const bool shouldCapIfCamel = wordStart && (toUpper || !inputStart);
62     if ((toSnake && shouldCapIfSnake) || (!toSnake && shouldCapIfCamel)) {
63       result += toupper(c);
64     } else {
65       result += tolower(c);
66     }
67 
68     // at this point we are no longer at the start of a word:
69     wordStart = false;
70     // .. or the input:
71     inputStart = false;
72   }
73 
74   if (wordStart) {
75     // This only happens with a trailing delimiter, which should remain.
76     result.push_back(delimiter);
77   }
78 
79   return result;
80 }
81 
82 }  // namespace
83 
84 //
85 // Public interface
86 //
87 
toLowerCamel(const string & s,const char delimiter)88 string toLowerCamel(const string &s, const char delimiter) {
89   return FormatStringCase(s, LOWER_CAMEL, delimiter);
90 }
toLowerSnake(const string & s,const char delimiter)91 string toLowerSnake(const string &s, const char delimiter) {
92   return FormatStringCase(s, LOWER_SNAKE, delimiter);
93 }
toUpperCamel(const string & s,const char delimiter)94 string toUpperCamel(const string &s, const char delimiter) {
95   return FormatStringCase(s, UPPER_CAMEL, delimiter);
96 }
toUpperSnake(const string & s,const char delimiter)97 string toUpperSnake(const string &s, const char delimiter) {
98   return FormatStringCase(s, UPPER_SNAKE, delimiter);
99 }
100 
101 }  // namespace generator
102 }  // namespace tensorflow
103