|
1 | 1 | package com.scriptbasic.utility.functions; |
2 | 2 |
|
3 | 3 | import com.scriptbasic.api.BasicFunction; |
| 4 | +import com.scriptbasic.executors.rightvalues.BasicStringValue; |
4 | 5 | import com.scriptbasic.interfaces.BasicRuntimeException; |
| 6 | +import com.scriptbasic.utility.RightValueUtility; |
5 | 7 |
|
6 | 8 | /** |
7 | 9 | * <p> |
@@ -49,74 +51,116 @@ static public String chomp(final String s) { |
49 | 51 | return s.replaceAll("\\n*$", ""); |
50 | 52 | } |
51 | 53 |
|
| 54 | + /** |
| 55 | + * Returns string for the argument. |
| 56 | + * @param o argument to be converted |
| 57 | + * @return string value |
| 58 | + * @throws BasicRuntimeException failed to convert to string |
| 59 | + */ |
| 60 | + @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
| 61 | + com.scriptbasic.classification.Utility.class}) |
| 62 | + static public String cstr(final Object o) throws BasicRuntimeException { |
| 63 | + if (o == null) { |
| 64 | + throw new BasicRuntimeException("Null cannot be converted to string"); |
| 65 | + } |
| 66 | + if (o instanceof String) { |
| 67 | + return (String) o; |
| 68 | + } |
| 69 | + return BasicStringValue.asString(RightValueUtility.createRightValue(o)); |
| 70 | + } |
| 71 | + |
52 | 72 | /** |
53 | 73 | * Trim the white spaces from the start of the string. |
54 | 74 | * |
55 | | - * @param s the string to trim. |
| 75 | + * @param o the string to trim. |
56 | 76 | * @return the trimmed string |
| 77 | + * @throws BasicRuntimeException if cannot convert to string |
57 | 78 | */ |
58 | 79 | @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
59 | 80 | com.scriptbasic.classification.Utility.class}) |
60 | | - static public String ltrim(final String s) { |
| 81 | + static public String ltrim(final Object o) throws BasicRuntimeException { |
| 82 | + if (o == null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + final String s = cstr(o); |
61 | 86 | return s.replaceAll("^\\s*", ""); |
62 | 87 | } |
63 | 88 |
|
64 | 89 | /** |
65 | 90 | * Trim the white spaces from the end of the string. |
66 | 91 | * |
67 | | - * @param s the string to trim |
| 92 | + * @param o the string to trim |
68 | 93 | * @return the trimmed string |
| 94 | + * @throws BasicRuntimeException if cannot convert to string |
69 | 95 | */ |
70 | 96 | @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
71 | 97 | com.scriptbasic.classification.Utility.class}) |
72 | | - static public String rtrim(final String s) { |
| 98 | + static public String rtrim(final Object o) throws BasicRuntimeException { |
| 99 | + if (o == null) { |
| 100 | + return null; |
| 101 | + } |
| 102 | + final String s = cstr(o); |
73 | 103 | return s.replaceAll("\\s*$", ""); |
74 | 104 | } |
75 | 105 |
|
76 | 106 | /** |
77 | 107 | * Return {@code len} number of characters from the left (the beginning) of the |
78 | 108 | * string. |
79 | 109 | * |
80 | | - * @param s parameter |
| 110 | + * @param o parameter |
81 | 111 | * @param len parameter |
82 | 112 | * @return return value |
| 113 | + * @throws BasicRuntimeException if cannot convert to string |
83 | 114 | */ |
84 | 115 | @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
85 | 116 | com.scriptbasic.classification.Utility.class}) |
86 | | - static public String left(final String s, final int len) { |
| 117 | + static public String left(final Object o, final int len) throws BasicRuntimeException { |
| 118 | + if (o == null) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + final String s = cstr(o); |
87 | 122 | return s.length() > len ? s.substring(0, len) : s; |
88 | 123 | } |
89 | 124 |
|
90 | 125 | /** |
91 | 126 | * Return a substring from the string that starts at the position |
92 | 127 | * {@code start} and has a length of {@code len}. |
93 | 128 | * |
94 | | - * @param s parameter |
| 129 | + * @param o parameter |
95 | 130 | * @param start parameter |
96 | 131 | * @param len parameter |
97 | 132 | * @return return value |
98 | 133 | * @throws BasicRuntimeException incorrect parameter |
99 | 134 | */ |
100 | 135 | @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
101 | 136 | com.scriptbasic.classification.Utility.class}) |
102 | | - static public String mid(final String s, final int start, final int len) throws BasicRuntimeException { |
| 137 | + static public String mid(final Object o, final int start, final int len) throws BasicRuntimeException { |
103 | 138 | if (start < 1) { |
104 | 139 | throw new BasicRuntimeException("Incorrect value in parameter start: " + start); |
105 | 140 | } |
| 141 | + if (o == null) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + final String s = cstr(o); |
106 | 145 | return s.substring(start - 1, start - 1 + len); |
107 | 146 | } |
108 | 147 |
|
109 | 148 | /** |
110 | 149 | * Return {@code len} number of characters from the right (the end) of the |
111 | 150 | * string. |
112 | 151 | * |
113 | | - * @param s parameter |
| 152 | + * @param o parameter |
114 | 153 | * @param len parameter |
115 | 154 | * @return return value |
| 155 | + * @throws BasicRuntimeException if cannot convert to string |
116 | 156 | */ |
117 | 157 | @BasicFunction(classification = {com.scriptbasic.classification.String.class, |
118 | 158 | com.scriptbasic.classification.Utility.class}) |
119 | | - static public String right(final String s, final int len) { |
| 159 | + static public String right(final Object o, final int len) throws BasicRuntimeException { |
| 160 | + if (o == null) { |
| 161 | + return null; |
| 162 | + } |
| 163 | + final String s = cstr(o); |
120 | 164 | return s.length() > len ? s.substring(s.length() - len) : s; |
121 | 165 | } |
122 | 166 |
|
@@ -185,7 +229,11 @@ static public String lcase(final String s) { |
185 | 229 | } |
186 | 230 |
|
187 | 231 | @BasicFunction(classification = {com.scriptbasic.classification.String.class}) |
188 | | - static public String trim(final String s) { |
| 232 | + static public String trim(final Object o) throws BasicRuntimeException { |
| 233 | + if (o == null) { |
| 234 | + return null; |
| 235 | + } |
| 236 | + final String s = cstr(o); |
189 | 237 | return s.trim(); |
190 | 238 | } |
191 | 239 |
|
|
0 commit comments