• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Follows convention set in objectivec_helpers.cc in the protobuf ObjC compiler.
2_upper_segments_list = ["url", "http", "https"]
3
4def strip_extension(str):
5    return str.rpartition(".")[0]
6
7def capitalize(word):
8    if word in _upper_segments_list:
9        return word.upper()
10    else:
11        return word.capitalize()
12
13def lower_underscore_to_upper_camel(str):
14    str = strip_extension(str)
15    camel_case_str = ""
16    word = ""
17    for c in str.elems():  # NB: assumes ASCII!
18        if c.isalpha():
19            word += c.lower()
20        else:
21            # Last word is finished.
22            if len(word):
23                camel_case_str += capitalize(word)
24                word = ""
25            if c.isdigit():
26                camel_case_str += c
27
28            # Otherwise, drop the character. See UnderscoresToCamelCase in:
29            # third_party/protobuf/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc
30
31    if len(word):
32        camel_case_str += capitalize(word)
33    return camel_case_str
34
35def file_to_upper_camel(src):
36    elements = src.rpartition("/")
37    upper_camel = lower_underscore_to_upper_camel(elements[-1])
38    return "".join(list(elements[:-1]) + [upper_camel])
39
40def file_with_extension(src, ext):
41    elements = src.rpartition("/")
42    return "".join(list(elements[:-1]) + [elements[-1], "." + ext])
43
44def to_upper_camel_with_extension(src, ext):
45    src = file_to_upper_camel(src)
46    return file_with_extension(src, ext)
47