view src/main/java/alice/test/codesegment/local/wordcount/WordCount.java @ 467:6e304a7a60e7 dispose

remove white space
author sugi
date Sat, 22 Nov 2014 12:08:24 +0900
parents aefbe41fcf12
children 15eeb439830c
line wrap: on
line source

package alice.test.codesegment.local.wordcount;

import alice.codesegment.CodeSegment;
import alice.datasegment.CommandType;
import alice.datasegment.Receiver;

public class WordCount extends CodeSegment{

    private Receiver info1 = ids.create(CommandType.TAKE);

    public WordCount(){
        info1.setKey("array");
    }

    @Override
    public void run() {
        Range r = info1.asClass(Range.class);
        int word_flag = 0;
        int word_num = 0;
        int line_num = 0;
        int i = 0;
        for (; i < r.array.length; i++) {
            if (r.array[i] == 0x20) { // 空白
                word_flag = 1;
            } else if (r.array[i] == 0x0A) { // 改行
                line_num += 1;
                word_flag = 1;
            } else {
                word_num += word_flag;
                word_flag = 0;
            }
        }
        word_num += word_flag;
        if (r.nextchar!='\0'){ // null means last block
            if ((r.array[i-1] == 0x0A||r.array[i-1] == 0x20) &&
                    (r.nextchar == 0x0A||r.nextchar == 0x20)){
                word_num--;
            }
        }

        Result result = new Result(line_num,word_num);
        ods.put("result", result);
        recycle();//new WordCount();

    }

}