|
| 1 | +// Copyright 2024 Google Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package google |
| 15 | + |
| 16 | +import ( |
| 17 | + "regexp" |
| 18 | + "strings" |
| 19 | +) |
| 20 | + |
| 21 | +// // Helper class to process and mutate strings. |
| 22 | +// class StringUtils |
| 23 | +// // Converts string from camel case to underscore |
| 24 | +// def self.underscore(source) |
| 25 | +// source.gsub(/::/, '/') |
| 26 | +// .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') |
| 27 | +// .gsub(/([a-z\d])([A-Z])/, '\1_\2') |
| 28 | +// .tr('-', '_') |
| 29 | +// .tr('.', '_') |
| 30 | +// .downcase |
| 31 | +// end |
| 32 | + |
| 33 | +// // Converts from PascalCase to Space Separated |
| 34 | +// def self.space_separated(source) |
| 35 | +// tmp = source.gsub(/([A-Z]+)([A-Z][a-z])/, '\1 \2') |
| 36 | +// .gsub(/([a-z\d])([A-Z])/, '\1 \2') |
| 37 | +// .downcase |
| 38 | +// tmp[0].upcase.concat(tmp[1..]) |
| 39 | +// end |
| 40 | + |
| 41 | +// Converts from PascalCase to Space Separated |
| 42 | +// For example, converts "AccessApproval" to "Access Approval" |
| 43 | +func SpaceSeparated(source string) string { |
| 44 | + tmp := regexp.MustCompile(`([A-Z]+)([A-Z][a-z])`).ReplaceAllString(source, "${1} ${2}") |
| 45 | + tmp = regexp.MustCompile(`([a-z\d])([A-Z])`).ReplaceAllString(tmp, "${1} ${2}") |
| 46 | + tmp = strings.ToLower(tmp) |
| 47 | + tmp = strings.Title(tmp) |
| 48 | + return tmp |
| 49 | +} |
| 50 | + |
| 51 | +// // Converts a string to space-separated capitalized words |
| 52 | +// def self.title(source) |
| 53 | +// ss = space_separated(source) |
| 54 | +// ss.gsub(/\b(?<!\w['’`()])[a-z]/, &:capitalize) |
| 55 | +// end |
| 56 | + |
| 57 | +// // rubocop:disable Style/SafeNavigation // support Ruby < 2.3.0 |
| 58 | +// def self.symbolize(key) |
| 59 | +// key.to_sym unless key.nil? |
| 60 | +// end |
| 61 | +// // rubocop:enable Style/SafeNavigation |
| 62 | + |
| 63 | +// // Returns all the characters up until the period (.) or returns text |
| 64 | +// // unchanged if there is no period. |
| 65 | +// def self.first_sentence(text) |
| 66 | +// period_pos = text.index(/[.?!]/) |
| 67 | +// return text if period_pos.nil? |
| 68 | + |
| 69 | +// text[0, period_pos + 1] |
| 70 | +// end |
| 71 | + |
| 72 | +// // Returns the plural form of a word |
| 73 | +// def self.plural(source) |
| 74 | +// // policies -> policies |
| 75 | +// // indices -> indices |
| 76 | +// return source if source.end_with?('ies') || source.end_with?('es') |
| 77 | + |
| 78 | +// // index -> indices |
| 79 | +// return "//{source.gsub(/ex$/, '')}ices" if source.end_with?('ex') |
| 80 | + |
| 81 | +// // mesh -> meshes |
| 82 | +// return "//{source}es" if source.end_with?('esh') |
| 83 | + |
| 84 | +// // key -> keys |
| 85 | +// // gateway -> gateways |
| 86 | +// return "//{source}s" if source.end_with?('ey') || source.end_with?('ay') |
| 87 | + |
| 88 | +// // policy -> policies |
| 89 | +// return "//{source.gsub(/y$/, '')}ies" if source.end_with?('y') |
| 90 | + |
| 91 | +// "//{source}s" |
| 92 | +// end |
| 93 | + |
| 94 | +// // Slimmed down version of ActiveSupport::Inflector code |
| 95 | +// def self.camelize(term, uppercase_first_letter) |
| 96 | +// acronyms_camelize_regex = /^(?:(?=a)b(?=\b|[A-Z_])|\w)/ |
| 97 | + |
| 98 | +// string = term.to_s |
| 99 | +// string = if uppercase_first_letter |
| 100 | +// string.sub(/^[a-z\d]*/) { |match| match.capitalize! || match } |
| 101 | +// else |
| 102 | +// string.sub(acronyms_camelize_regex) { |match| match.downcase! || match } |
| 103 | +// end |
| 104 | +// // handle snake case |
| 105 | +// string.gsub!(/(?:_)([a-z\d]*)/i) do |
| 106 | +// word = ::Regexp.last_match(1) |
| 107 | +// word.capitalize! || word |
| 108 | +// end |
| 109 | +// string |
| 110 | +// end |
| 111 | +// end |
0 commit comments