stringToValue method

bool stringToValue(
  1. ClipParam param,
  2. String string,
  3. MathValue value
)

Implementation

bool stringToValue( ClipParam param, String string, MathValue value ){
	int i, j;
	bool swi;
	int top;
	ParamInteger stop = ParamInteger();
	List<double> tmp = List.filled( 4, 0.0 );

	top = ClipGlobal.isCharEscape( string, 0 ) ? 1 : 0;
	switch( ClipMath.charAt( string, top ) ){
	case '+': top++  ; swi = false; break;
	case '-': top++  ; swi = true ; break;
	default : top = 0; swi = false; break;
	}

	if( ClipMath.charAt( string, top ) == '\'' ){
		value.ass( 0.0 );
		j = 0;
		for( i = 1; ; i++ ){
			if( top + i >= string.length ){
				break;
			}
			if( ClipGlobal.isCharEscape( string, top + i ) ){
				i++;
				if( top + i >= string.length ){
					break;
				}
				switch( ClipMath.charAt( string, top + i ) ){
				case 'b': tmp[0] = ClipMath.char( '\b' ).toDouble(); break;
				case 'f': tmp[0] = ClipMath.char( '\f' ).toDouble(); break;
				case 'n': tmp[0] = ClipMath.char( '\n' ).toDouble(); break;
				case 'r': tmp[0] = ClipMath.char( '\r' ).toDouble(); break;
				case 't': tmp[0] = ClipMath.char( '\t' ).toDouble(); break;
				case 'v': tmp[0] = ClipMath.char( '\v' ).toDouble(); break;
				default : tmp[0] = ClipMath.charCodeAt( string, top + i ).toDouble(); break;
				}
			} else {
				tmp[0] = ClipMath.charCodeAt( string, top + i ).toDouble();
			}
			value.ass( value.toFloat() * 256 + tmp[0] );
			j++;
			if( j >= 4 ){
				break;
			}
		}
		if( swi ){
			value.ass( value.minus() );
		}
	} else if( ClipGlobal.isCharEscape( string, top ) ){
		switch( ClipMath.charAt( string, top + 1 ) ){
		case 'b':
		case 'B':
			value.ass( ClipMath.stringToInt( string, top + 2, stop, 2 ) );
			break;
		case '0':
			value.ass( ClipMath.stringToInt( string, top + 2, stop, 8 ) );
			break;
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			value.ass( ClipMath.stringToInt( string, top + 1, stop, 10 ) );
			break;
		case 'x':
		case 'X':
			value.ass( ClipMath.stringToInt( string, top + 2, stop, 16 ) );
			break;
		default:
			return false;
		}
		if( stop.val() < string.length ){
			return false;
		}
		if( swi ){
			value.ass( value.minus() );
		}
	} else {
		if( (param.mode() & ClipGlobal.modeComplex) != 0 ){
			tmp[0] = ClipMath.stringToFloat( string, top, stop );
			switch( ClipMath.charAt( string, stop.val() ) ){
			case '\\':
			case ClipGlobal.charUtf8Yen:
			case '+':
			case '-':
				// 実数部
				if( stop.val() == top ){
					return false;
				}
				value.setReal( swi ? -tmp[0] : tmp[0] );

				// 虚数部
				if( ClipGlobal.isCharEscape( string, stop.val() ) ){
					stop.add( 1 );
				}
				switch( ClipMath.charAt( string, stop.val() ) ){
				case '+': swi = false; break;
				case '-': swi = true ; break;
				default : return false;
				}
				top = stop.val() + 1;
				tmp[0] = ClipMath.stringToFloat( string, top, stop );
				if( (ClipMath.charAt( string, stop.val() ) != 'i') && (ClipMath.charAt( string, stop.val() ) != 'I') ){
					return false;
				} else {
					if( stop.val() + 1 < string.length ){
						return false;
					}
					if( stop.val() == top ){
						value.setImag( swi ? -1.0 : 1.0 );
					} else {
						value.setImag( swi ? -tmp[0] : tmp[0] );
					}
				}

				break;
			case 'i':
			case 'I':
				if( stop.val() + 1 < string.length ){
					return false;
				}

				// 実数部
				value.setReal( 0.0 );

				// 虚数部
				if( stop.val() == top ){
					value.setImag( swi ? -1.0 : 1.0 );
				} else {
					value.setImag( swi ? -tmp[0] : tmp[0] );
				}

				break;
			default:
				if( stop.val() == top ){
					return false;
				}
				value.ass( swi ? -tmp[0] : tmp[0] );
				if( stop.val() < string.length ){
					switch( ClipMath.charAt( string, stop.val() ) ){
					case 'd': case 'D': value.angToAng( ClipMath.angTypeDeg , ClipMath.complexAngType() ); break;
					case 'g': case 'G': value.angToAng( ClipMath.angTypeGrad, ClipMath.complexAngType() ); break;
					case 'r': case 'R': value.angToAng( ClipMath.angTypeRad , ClipMath.complexAngType() ); break;
					default : return false;
					}
				}
				break;
			}
		} else if( (param.mode() & (ClipGlobal.modeFFloat | ClipGlobal.modeFract)) != 0 ){
			tmp[0] = ClipMath.stringToFloat( string, top, stop );
			switch( ClipMath.charAt( string, stop.val() ) ){
			case '_':
			case ClipGlobal.charFract:
				if( stop.val() == top ){
					return false;
				}
				value.fractSetMinus( swi );
				value.setNum( tmp[0] );

				if( ClipGlobal.isCharEscape( string, stop.val() + 1 ) ){
					top = stop.val() + 2;
				} else {
					top = stop.val() + 1;
				}
				tmp[0] = ClipMath.stringToFloat( string, top, stop );
				switch( ClipMath.charAt( string, stop.val() ) ){
				case '_':
				case ClipGlobal.charFract:
					if( stop.val() == top ){
						return false;
					}

					if( ClipGlobal.isCharEscape( string, stop.val() + 1 ) ){
						top = stop.val() + 2;
					} else {
						top = stop.val() + 1;
					}
					tmp[1] = ClipMath.stringToFloat( string, top, stop );
					if( (tmp[0] < 0.0) || (tmp[1] < 0.0) ){
						return false;
					}
					value.setDenom( tmp[1] );
					value.setNum  ( value.num() * value.denom() + tmp[0] );
					value.fractReduce();
					break;
				default:
					if( tmp[0] < 0.0 ){
						return false;
					}
					value.setDenom( tmp[0] );
					value.fractReduce();
					break;
				}
				break;
			default:
				if( stop.val() == top ){
					return false;
				}
				value.ass( swi ? -tmp[0] : tmp[0] );
				break;
			}
			if( stop.val() < string.length ){
				switch( ClipMath.charAt( string, stop.val() ) ){
				case 'd': case 'D': value.angToAng( ClipMath.angTypeDeg , ClipMath.complexAngType() ); break;
				case 'g': case 'G': value.angToAng( ClipMath.angTypeGrad, ClipMath.complexAngType() ); break;
				case 'r': case 'R': value.angToAng( ClipMath.angTypeRad , ClipMath.complexAngType() ); break;
				default : return false;
				}
			}
		} else if( (param.mode() & ClipGlobal.modeTime) != 0 ){
			bool _break = false;
			for( i = 0; i < 4; i++ ){
				if( ClipGlobal.isCharEscape( string, top ) ){
					top++;
				}
				tmp[i] = ClipMath.stringToFloat( string, top, stop );
				if( stop.val() == top ){
					return false;
				}
				if( stop.val() >= string.length ){
					break;
				}
				switch( ClipMath.charAt( string, stop.val() ) ){
				case 'h':
				case 'H':
				case 'm':
				case 'M':
				case 's':
				case 'S':
				case 'f':
				case 'F':
					if( stop.val() + 1 < string.length ){
						return false;
					}
					_break = true;
					break;
				case ':':
					break;
				default:
					return false;
				}
				if( _break ){
					break;
				}
				top = stop.val() + 1;
			}
			value.timeSetMinus( swi );
			switch( i ){
			case 0:
				if( stop.val() < string.length ){
					switch( ClipMath.charAt( string, stop.val() ) ){
					case 'h': case 'H': value.setHour ( tmp[0] ); value.timeReduce(); break;
					case 'm': case 'M': value.setMin  ( tmp[0] ); value.timeReduce(); break;
					case 's': case 'S': value.setSec  ( tmp[0] ); value.timeReduce(); break;
					case 'f': case 'F': value.setFrame( tmp[0] ); value.timeReduce(); break;
					}
				} else {
					value.setSec( tmp[0] );
					value.timeReduce();
				}
				break;
			case 1:
				if( stop.val() < string.length ){
					switch( ClipMath.charAt( string, stop.val() ) ){
					case 'h': case 'H': return false;
					case 'm': case 'M': value.setHour( tmp[0] ); value.setMin  ( tmp[1] ); value.timeReduce(); break;
					case 's': case 'S': value.setMin ( tmp[0] ); value.setSec  ( tmp[1] ); value.timeReduce(); break;
					case 'f': case 'F': value.setSec ( tmp[0] ); value.setFrame( tmp[1] ); value.timeReduce(); break;
					}
				} else {
					switch( param.mode() & ClipGlobal.modeMask ){
					case ClipGlobal.modeHTime:
					case ClipGlobal.modeMTime: value.setHour( tmp[0] ); value.setMin  ( tmp[1] ); value.timeReduce(); break;
					case ClipGlobal.modeSTime: value.setMin ( tmp[0] ); value.setSec  ( tmp[1] ); value.timeReduce(); break;
					case ClipGlobal.modeFTime: value.setSec ( tmp[0] ); value.setFrame( tmp[1] ); value.timeReduce(); break;
					}
				}
				break;
			case 2:
				if( stop.val() < string.length ){
					switch( ClipMath.charAt( string, stop.val() ) ){
					case 'h': case 'H':
					case 'm': case 'M': return false;
					case 's': case 'S': value.setHour( tmp[0] ); value.setMin( tmp[1] ); value.setSec  ( tmp[2] ); value.timeReduce(); break;
					case 'f': case 'F': value.setMin ( tmp[0] ); value.setSec( tmp[1] ); value.setFrame( tmp[2] ); value.timeReduce(); break;
					}
				} else {
					switch( param.mode() & ClipGlobal.modeMask ){
					case ClipGlobal.modeHTime:
					case ClipGlobal.modeMTime:
					case ClipGlobal.modeSTime: value.setHour( tmp[0] ); value.setMin( tmp[1] ); value.setSec  ( tmp[2] ); value.timeReduce(); break;
					case ClipGlobal.modeFTime: value.setMin ( tmp[0] ); value.setSec( tmp[1] ); value.setFrame( tmp[2] ); value.timeReduce(); break;
					}
				}
				break;
			case 3:
				if( stop.val() < string.length ){
					switch( ClipMath.charAt( string, stop.val() ) ){
					case 'h': case 'H':
					case 'm': case 'M':
					case 's': case 'S': return false;
					case 'f': case 'F': value.setHour( tmp[0] ); value.setMin( tmp[1] ); value.setSec( tmp[2] ); value.setFrame( tmp[3] ); value.timeReduce(); break;
					}
				} else {
					switch( param.mode() & ClipGlobal.modeMask ){
					case ClipGlobal.modeHTime:
					case ClipGlobal.modeMTime:
					case ClipGlobal.modeSTime:
					case ClipGlobal.modeFTime: value.setHour( tmp[0] ); value.setMin( tmp[1] ); value.setSec( tmp[2] ); value.setFrame( tmp[3] ); value.timeReduce(); break;
					}
				}
				break;
			}
		} else if( (param.mode() & ClipGlobal.modeInt) != 0 ){
			value.ass( ClipMath.stringToInt( string, top, stop, param.radix() ) );
			if( stop.val() < string.length ){
				return false;
			}
			if( swi ){
				value.ass( value.minus() );
			}
		}
	}

	return true;
}