上海公交真烂上海公交真烂上海公交真烂上海公交真烂上海公交真烂上海公交真烂上海公交真烂上海公交真烂
前几天用split的时候发现一个programming ruby里没提到的用法, 刚刚开始还吓了一大跳..
不知道大家知道不, 分享一下先
"aa,bb,cc".split(/(,)/)
结果是['aa', ',', 'bb', ',', 'cc']
而不是期望中的 ['aa', 'bb', 'cc']
原来正则表达式中的括号搞的鬼
如果写成"aa,bb,cc".split(/,/)就是会得到期望中的结果了
看到有前辈写了一个
UTF-8与UNICODE相互转换的代码
,
顺便提一下,希望可以给大家提供一点帮助.
下面是一些编码格式的bit长
Examples of fixed-width encoding forms:
Type
|
Each character encoded as
|
Notes
|
7-bit
|
a single 7-bit quantity
|
example:
ISO
646
|
8-bit G0/G1
|
a single 8-bit quantity
|
with constraints on use of C0 and C1 spaces
|
8-bit
|
a single 8-bit quantity
|
with no constraints on use of C1 space
|
8-bit
EBCDIC
|
a single 8-bit quantity
|
with the EBCDIC conventions rather than
ASCII
conventions
|
16-bit (
UCS
-2)
|
a single 16-bit quantity
|
within a code space of 0..FFFF
|
32-bit (
UCS
-4)
|
a single 32-bit quantity
|
within a code space 0..7FFFFFFF
|
32-bit (
UTF
-32)
|
a single 32-bit quantity
|
within a code space of 0..10FFFF
|
16-bit
DBCS
process code
|
a single 16-bit quantity
|
example: UNIX widechar implementations of Asian CCS's
|
32-bit
DBCS
process code
|
a single 32-bit quantity
|
example: UNIX widechar implementations of Asian CCS's
|
DBCS
Host
|
two 8-bit quantities
|
following IBM host conventions
|
Examples of variable-width encoding forms:
Name
|
Characters are encoded as
|
Notes
|
UTF
-8
|
a mix of one to four 8-bit code units in Unicode and one to six code units in 10646
|
used only with Unicode/10646
|
UTF
-16
|
a mix of one to two 16 bit code units
|
used only with Unicode/10646
|
Boost中提供了一个UTF-8 Codecvt Facet,可以在utf8和UCS-4(Unicode-32)之间转换.
使用方式如下
//...
// My encoding type
typedef wchar_t ucs4_t;
std::locale old_locale;
std::locale utf8_locale(old_locale,new utf8_codecvt_facet<ucs4_t>);
// Set a New global locale
std::locale::global(utf8_locale);
// UCS-4 转换为 UTF-8
{
std::wofstream ofs("data.ucd");
ofs.imbue(utf8_locale);
std::copy(ucs4_data.begin(),ucs4_data.end(),
std::ostream_iterator<ucs4_t,ucs4_t>(ofs));
}
// 读入 UTF-8 ,转换为 UCS-4
std::vector<ucs4_t> from_file;
{
std::wifstream ifs("data.ucd");
ifs.imbue(utf8_locale);
ucs4_t item = 0;
while (ifs >> item) from_file.push_back(item);
}
//...
UTF-8 Codecvt Facet详见
http://www.boost.org/libs/serialization/doc/codecvt.html
watir 处理弹出窗口
[fxzeng 原创]
(转载请保留此标志)
fxzeng
mail: fxzeng@126.com
处理弹出窗口是进行web测试的必经之路, 对于对于初学者这似乎是一个并不太好解决的问题, watir 自带的unittests里有这样的例子,但是牵扯的文件多, 而且还涉及到块的调用, 往往初学者只能照葫芦画瓢的解决这个问题.
这里介绍一种简便的处理方法, 先看看unittests中是如何处理弹出窗口的:
共牵扯到三个文件
#-----------------unittests/jscript_test.rb-------------------------------
# check_dialog, to create a new thread to help push the dialog button
#
def check_dialog(extra_file, expected_result, &block)
goto_javascript_page()
Thread.new { system("rubyw \"#{$mydir}\\#{extra_file}.rb\"") }
# system(), call win cmd to run
jscriptExtraAlert.rb
block.call
testResult = $ie.text_field(:id, "testResult").value
assert_match( expected_result, testResult )
end
# test method
def test_alert_button()
check_dialog('jscriptExtraAlert', /Alert OK/){ $ie.button(:id, 'btnAlert').click }
end
#--------------END--jscript_test.rb-------------------------------
#---------------unittests/jscriptExtraAlert.rb-------------------------------
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 == __FILE__
require 'watir/WindowHelper'
helper = WindowHelper.new
helper.push_alert_button()
#-------------END--jscriptExtraAlert.rb-------------------------------
#-------------watir/WindowHelper.rb------------------------------
require 'win32ole'
class WindowHelper
def initialize( )
@autoit = WIN32OLE.new('AutoItX3.Control')
end
def push_alert_button()
@autoit.WinWait "Microsoft Internet Explorer", ""
@autoit.Send "{ENTER}"
end
#------------END--WindowHelper.rb--------------------------
文件之间的调用关系真是够复杂的, : ) 涉及到了三个文件, 如果需要按Cancel键的话还得增加一个jscriptExtraConfirmCancel.rb, 需要处理安全警告的话还得增加一个...., 或许你会说, 这些文件都不复杂, 写在主文件里不就行了么, 干吗还得调用文件?? 呵呵, 试试就知道为什么了...
这里介绍的方法用一个文件就可以代替原来的多个文件, 即可以处理确认, 撤消, 安全警告, 选择文件等多种情况的弹出窗口.
#----------main-method-----------------------------
def test_push_button
... ...
thrs = []
thrs << Thread.new{system("rubyw #{mydir}\\myWinHelper.rb Microsoft delete TAB ENTER")}
# Microsoft, title of the popup window, delete, text contained in the window, TAB ENTER, push TAB key then ENTER
thrs << Thread.new{$ie.button(:id, 'btnAlert').click }
thrs.each{|x| x.join}
... ...
end
#---------END--main-method--------------------
#--------myWinHelper.rb-----------------------
require 'win32ole'
autoit = WIN32OLE.new"AutoItX3.Control"
title = ARGV[0]
texts = ARGV[1]
operation = []
for i in 2..(ARGV.lengh - 3)
operaton << ARGV[i]
end
autoit.WinWait(title,texts)
operatoin.each{|x|
autoit.Send"{x}"
}
#---------END--myWinHelper.rb------------
这样, 弹出窗口问题就可以轻松解决了, 只需一个myWinHelper.rb就可以处理确定取消等常见问题, 这里的myWinHelper.rb只是一个例子, 如果有兴趣, 可以在里面添加语句, 可以轻松处理更多问题, 比如
安全警告窗口:
thrs << Thread.new{system("rubyw #{mydir}\\myWinHelper.rb \"TAB 3\" ENTER")}
# push TAB key three times, and ENTER
选择文件窗口:
thrs << Thread.new{system("rubyw #{mydir}\\myWinHelper.rb d:\\file\\test.txt !o")}
# enter the directory of the file, and then push Alt+o
watir 处理弹出窗口 [fxzeng 原创]
fxzeng
mail: fxzeng@126.com