| 1 |
/*======================================================================*//** |
|---|
| 2 |
* |
|---|
| 3 |
* Utils for ActionScript 3.0 |
|---|
| 4 |
* |
|---|
| 5 |
* @author Copyright (c) 2007 Spark project. |
|---|
| 6 |
* @version 1.0.0 |
|---|
| 7 |
* |
|---|
| 8 |
* @see http://utils.libspark.org/ |
|---|
| 9 |
* @see http://www.libspark.org/ |
|---|
| 10 |
* |
|---|
| 11 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 12 |
* you may not use this file except in compliance with the License. |
|---|
| 13 |
* You may obtain a copy of the License at |
|---|
| 14 |
* |
|---|
| 15 |
* http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 16 |
* |
|---|
| 17 |
* Unless required by applicable law or agreed to in writing, software |
|---|
| 18 |
* distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 19 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
|---|
| 20 |
* either express or implied. See the License for the specific language |
|---|
| 21 |
* governing permissions and limitations under the License. |
|---|
| 22 |
* |
|---|
| 23 |
*//*=======================================================================*/ |
|---|
| 24 |
package org.libspark.utils { |
|---|
| 25 |
import flash.errors.IllegalOperationError; |
|---|
| 26 |
import flash.text.TextField; |
|---|
| 27 |
import flash.text.TextFormat; |
|---|
| 28 |
import flash.utils.ByteArray; |
|---|
| 29 |
|
|---|
| 30 |
/** |
|---|
| 31 |
* 文字列のためのユーティリティクラスです |
|---|
| 32 |
*/ |
|---|
| 33 |
public class StringUtil { |
|---|
| 34 |
|
|---|
| 35 |
/*======================================================================*//** |
|---|
| 36 |
* @private |
|---|
| 37 |
*//*=======================================================================*/ |
|---|
| 38 |
public function StringUtil() { |
|---|
| 39 |
throw new IllegalOperationError( "StringUtil クラスはインスタンスを生成できません。" ); |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
/*======================================================================*//** |
|---|
| 47 |
* String の最初の文字を大文字にし、以降の文字を小文字に変換して返します。 |
|---|
| 48 |
* @author taka:nium |
|---|
| 49 |
* @param str 変換したい String です。 |
|---|
| 50 |
* @return 変換後の String です。 |
|---|
| 51 |
*//*=======================================================================*/ |
|---|
| 52 |
static public function toUpperCaseFirstLetter( str:String ):String { |
|---|
| 53 |
return str.charAt( 0 ).toUpperCase() + str.slice( 1 ).toLowerCase(); |
|---|
| 54 |
} |
|---|
| 55 |
|
|---|
| 56 |
/*======================================================================*//** |
|---|
| 57 |
* 半角スペースを削除し、次の文字を大文字に変換します。 |
|---|
| 58 |
* @author taka:nium |
|---|
| 59 |
* michi at seyself.com |
|---|
| 60 |
* @param str 変換したい String です。 |
|---|
| 61 |
* @return 変換後の String です。 |
|---|
| 62 |
* @example 以下のコードでは action script 3.0 language reference という文字列を変換し出力ます。 |
|---|
| 63 |
* <listing> |
|---|
| 64 |
* var str:String = "action script 3.0 language reference"; |
|---|
| 65 |
* |
|---|
| 66 |
* trace(StringUtil.camelize(str)); |
|---|
| 67 |
* // output : ActionScript3.0LanguageReference |
|---|
| 68 |
* |
|---|
| 69 |
* trace(StringUtil.decamelize(StringUtil.camelize(str))); |
|---|
| 70 |
* // output : action script 3.0 language reference |
|---|
| 71 |
* </listing> |
|---|
| 72 |
*//*=======================================================================*/ |
|---|
| 73 |
static public function camelize( str:String ):String { |
|---|
| 74 |
return str.replace(/(\s|^)(\w)/g, |
|---|
| 75 |
function(...$):String { return $[2].toUpperCase(); }); |
|---|
| 76 |
} |
|---|
| 77 |
|
|---|
| 78 |
/*======================================================================*//** |
|---|
| 79 |
* 大文字の String を、区切り文字と小文字化した String に変換します。 |
|---|
| 80 |
* @author taka:nium |
|---|
| 81 |
* michi at seyself.com |
|---|
| 82 |
* @param str 変換したい String です。 |
|---|
| 83 |
* @param separater 区切り文字として使用したい String です。 |
|---|
| 84 |
* @return 変換後の String です。 |
|---|
| 85 |
* @example 以下のコードでは action script 3.0 language reference という文字列を変換し出力ます。 |
|---|
| 86 |
* <listing> |
|---|
| 87 |
* var str:String = "action script 3.0 language reference"; |
|---|
| 88 |
* |
|---|
| 89 |
* trace("output : " + StringUtil.camelize(str)); |
|---|
| 90 |
* // output : ActionScript3.0LanguageReference |
|---|
| 91 |
* |
|---|
| 92 |
* trace("output : " + StringUtil.decamelize(StringUtil.camelize(str))); |
|---|
| 93 |
* // output : action script 3.0 language reference |
|---|
| 94 |
* </listing> |
|---|
| 95 |
*//*=======================================================================*/ |
|---|
| 96 |
static public function decamelize( str:String, separater:String = " " ):String { |
|---|
| 97 |
return str.replace( /(([^.\d])(\d)|[A-Z])/g, |
|---|
| 98 |
function(...$):String { |
|---|
| 99 |
if($[2]) return $[2] + separater + $[3].toLowerCase(); |
|---|
| 100 |
if($[4]==0) return $[0].toLowerCase(); |
|---|
| 101 |
return separater + $[0].toLowerCase(); |
|---|
| 102 |
}); |
|---|
| 103 |
//return str.replace( new RegExp( "[a-z][A-Z]", "g" ), function():String { |
|---|
| 104 |
//return String( arguments[0] ).toLowerCase().split( "" ).join( separater ); |
|---|
| 105 |
//} ); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
/** |
|---|
| 109 |
* 改行コードをすべて\r(CR)に変換します。 |
|---|
| 110 |
* |
|---|
| 111 |
* @param str 変換対象の文字列 |
|---|
| 112 |
* @return 変換後の文字列を返します |
|---|
| 113 |
* @author michi at seyself.com |
|---|
| 114 |
*/ |
|---|
| 115 |
public static function replaceLineFeed( str:String ):String |
|---|
| 116 |
{ |
|---|
| 117 |
str = str.split("\r\n").join("\r"); |
|---|
| 118 |
return str.split("\n").join("\r"); |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
/** |
|---|
| 122 |
* 同じ文字列を複数連結した文字列を返します。 |
|---|
| 123 |
* |
|---|
| 124 |
* @param value 文字列 |
|---|
| 125 |
* @param len 連結数 |
|---|
| 126 |
* @return 新しい文字列を返します |
|---|
| 127 |
* @author michi at seyself.com |
|---|
| 128 |
*/ |
|---|
| 129 |
public static function strpow( value:String , len:uint ):String |
|---|
| 130 |
{ |
|---|
| 131 |
var tmp:String = ""; |
|---|
| 132 |
for(var i:int=0;i<len;i++) tmp += value; |
|---|
| 133 |
return tmp; |
|---|
| 134 |
} |
|---|
| 135 |
|
|---|
| 136 |
/** |
|---|
| 137 |
* テキストフィールドの横幅を維持するためにtextプロパティに指定されている文字列の末尾を削ります。 |
|---|
| 138 |
* テキストフィールドが単一行の設定でなければ効果はありません。 |
|---|
| 139 |
* |
|---|
| 140 |
* @param textField 対象となるテキストフィールド |
|---|
| 141 |
* @param width 制限する横幅 |
|---|
| 142 |
* @param param 末尾を3点リーダ等に置き換える場合に指定する文字列 |
|---|
| 143 |
* @author michi at seyself.com |
|---|
| 144 |
*/ |
|---|
| 145 |
public static function fitTextField( textField:TextField , width:Number , param:String="" ):void |
|---|
| 146 |
{ |
|---|
| 147 |
if ( param == null ) param = ""; |
|---|
| 148 |
var n:uint = param.length + 1; |
|---|
| 149 |
var textFormat:TextFormat = textField.getTextFormat(); // 初期状態のTextFormatを記憶 by nagase at ngsdev.org |
|---|
| 150 |
while( textField.textWidth > width ){ |
|---|
| 151 |
if (textField.textWidth > width * 2) { |
|---|
| 152 |
textField.text = textField.text.substr(0, textField.text.length*0.66>>0) + param; |
|---|
| 153 |
} |
|---|
| 154 |
textField.text = textField.text.substr(0, textField.text.length - n) + param; |
|---|
| 155 |
textField.setTextFormat(textFormat); // TextFormatを復元 |
|---|
| 156 |
} |
|---|
| 157 |
} |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
} |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|