11package com .scriptbasic .utility .functions ;
22
33import com .scriptbasic .api .BasicFunction ;
4+ import com .scriptbasic .executors .rightvalues .BasicStringValue ;
45import com .scriptbasic .interfaces .BasicRuntimeException ;
6+ import com .scriptbasic .utility .RightValueUtility ;
57
68/**
79 * <p>
@@ -49,34 +51,56 @@ static public String chomp(final String s) {
4951 return s .replaceAll ("\\ n*$" , "" );
5052 }
5153
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+
5272 /**
5373 * Trim the white spaces from the start of the string.
5474 *
55- * @param s the string to trim.
75+ * @param o the string to trim.
5676 * @return the trimmed string
77+ * @throws BasicRuntimeException if cannot convert to string
5778 */
5879 @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
5980 com .scriptbasic .classification .Utility .class })
60- static public String ltrim (final Object s ) {
61- if (s ==null ) {
81+ static public String ltrim (final Object o ) throws BasicRuntimeException {
82+ if (o ==null ) {
6283 return null ;
6384 }
64- return s .toString ().replaceAll ("^\\ s*" , "" );
85+ final String s = cstr (o );
86+ return s .replaceAll ("^\\ s*" , "" );
6587 }
6688
6789 /**
6890 * Trim the white spaces from the end of the string.
6991 *
70- * @param s the string to trim
92+ * @param o the string to trim
7193 * @return the trimmed string
94+ * @throws BasicRuntimeException if cannot convert to string
7295 */
7396 @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
7497 com .scriptbasic .classification .Utility .class })
75- static public String rtrim (final Object s ) {
76- if ( s == null ) {
98+ static public String rtrim (final Object o ) throws BasicRuntimeException {
99+ if ( o == null ) {
77100 return null ;
78101 }
79- return s .toString ().replaceAll ("\\ s*$" , "" );
102+ final String s = cstr (o );
103+ return s .replaceAll ("\\ s*$" , "" );
80104 }
81105
82106 /**
@@ -86,14 +110,15 @@ static public String rtrim(final Object s) {
86110 * @param o parameter
87111 * @param len parameter
88112 * @return return value
113+ * @throws BasicRuntimeException if cannot convert to string
89114 */
90115 @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
91116 com .scriptbasic .classification .Utility .class })
92- static public String left (final Object o , final int len ) {
93- if ( o == null ) {
117+ static public String left (final Object o , final int len ) throws BasicRuntimeException {
118+ if ( o == null ) {
94119 return null ;
95120 }
96- String s = o . toString ( );
121+ final String s = cstr ( o );
97122 return s .length () > len ? s .substring (0 , len ) : s ;
98123 }
99124
@@ -113,10 +138,10 @@ static public String mid(final Object o, final int start, final int len) throws
113138 if (start < 1 ) {
114139 throw new BasicRuntimeException ("Incorrect value in parameter start: " + start );
115140 }
116- if ( o == null ) {
141+ if ( o == null ) {
117142 return null ;
118143 }
119- String s = o . toString ( );
144+ final String s = cstr ( o );
120145 return s .substring (start - 1 , start - 1 + len );
121146 }
122147
@@ -127,14 +152,15 @@ static public String mid(final Object o, final int start, final int len) throws
127152 * @param o parameter
128153 * @param len parameter
129154 * @return return value
155+ * @throws BasicRuntimeException if cannot convert to string
130156 */
131157 @ BasicFunction (classification = {com .scriptbasic .classification .String .class ,
132158 com .scriptbasic .classification .Utility .class })
133- static public String right (final Object o , final int len ) {
134- if ( o == null ) {
159+ static public String right (final Object o , final int len ) throws BasicRuntimeException {
160+ if ( o == null ) {
135161 return null ;
136162 }
137- String s = o . toString ( );
163+ final String s = cstr ( o );
138164 return s .length () > len ? s .substring (s .length () - len ) : s ;
139165 }
140166
@@ -203,11 +229,12 @@ static public String lcase(final String s) {
203229 }
204230
205231 @ BasicFunction (classification = {com .scriptbasic .classification .String .class })
206- static public String trim (final Object s ) {
207- if ( s == null ) {
232+ static public String trim (final Object o ) throws BasicRuntimeException {
233+ if ( o == null ) {
208234 return null ;
209235 }
210- return s .toString ().trim ();
236+ final String s = cstr (o );
237+ return s .trim ();
211238 }
212239
213240 /**
0 commit comments