1"""

2 String-handling utilities to avoid locale-dependence.

3

4 Used primarily to generate type name aliases.

5"""

6# "import string" is costly to import!

7# Construct the translation tables directly

8# "A" = chr(65), "a" = chr(97)

9_all_chars = [chr(_m) for _m in range(256)]

10_ascii_upper = _all_chars[65:65+26]

11_ascii_lower = _all_chars[97:97+26]

12 LOWER_TABLE = "".join(_all_chars[:65] + _ascii_lower + _all_chars[65+26:])

13 UPPER_TABLE = "".join(_all_chars[:97] + _ascii_upper + _all_chars[97+26:])

14

15

16 def english_lower(s):

17 """ Apply English case rules to convert ASCII strings to all lower case.

18

19 This is an internal utility function to replace calls to str.lower() such

20 that we can avoid changing behavior with changing locales. In particular,

21 Turkish has distinct dotted and dotless variants of the Latin letter "I" in

22 both lowercase and uppercase. Thus, "I".lower() != "i" in a "tr" locale.

23

24 Parameters

25 ----------

26 s : str

27

28 Returns

29 -------

30 lowered : str

31

32 Examples

33 --------

34 >>> from numpy.core.numerictypes import english_lower

35 >>> english_lower('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')

36 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789_'

37 >>> english_lower('')

38 ''

39 """

40 lowered = s.translate(LOWER_TABLE)

41 return lowered

42

43

44 def english_upper(s):

45 """ Apply English case rules to convert ASCII strings to all upper case.

46

47 This is an internal utility function to replace calls to str.upper() such

48 that we can avoid changing behavior with changing locales. In particular,

49 Turkish has distinct dotted and dotless variants of the Latin letter "I" in

50 both lowercase and uppercase. Thus, "i".upper() != "I" in a "tr" locale.

51

52 Parameters

53 ----------

54 s : str

55

56 Returns

57 -------

58 uppered : str

59

60 Examples

61 --------

62 >>> from numpy.core.numerictypes import english_upper

63 >>> english_upper('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_')

64 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'

65 >>> english_upper('')

66 ''

67 """

68 uppered = s.translate(UPPER_TABLE)

69 return uppered

70

71

72 def english_capitalize(s):

73 """ Apply English case rules to convert the first character of an ASCII

74 string to upper case.

75

76 This is an internal utility function to replace calls to str.capitalize()

77 such that we can avoid changing behavior with changing locales.

78

79 Parameters

80 ----------

81 s : str

82

83 Returns

84 -------

85 capitalized : str

86

87 Examples

88 --------

89 >>> from numpy.core.numerictypes import english_capitalize

90 >>> english_capitalize('int8')

91 'Int8'

92 >>> english_capitalize('Int8')

93 'Int8'

94 >>> english_capitalize('')

95 ''

96 """

97 if s:

98 return english_upper(s[0]) + s[1:]

99 else:

100 return s