将字节字符串解析为本机scala字节

时间:2016-01-20 02:35:02

标签: scala

我正在尝试将这样的字符串解析为"devDependencies": { "bower": "~1.3.1", "grunt": "~0.4.4", "grunt-autoprefixer": "~2.0.0", "grunt-cli": "~0.1.13", "grunt-contrib-jasmine": "~0.6.1", "grunt-karma": "~0.8.0", "grunt-karma-sonar": "~0.1.3", "istanbul": "~0.4.2", "jasmine-core": "~2.3.4", "karma": "~0.12.0", "karma-chrome-launcher": "~0.1.12", "karma-coverage": "~0.2.0", "karma-firefox-launcher": "~0.1.6", "karma-jasmine": "~0.2.1", "karma-junit-reporter": "~0.2.1", "karma-phantomjs-launcher": "~0.1.2", "karma-junit-sonarqube-reporter": "0.0.1", "load-grunt-tasks": "^3.4.0", "phantomjs": "~1.9.19", }

这是字符串

//require('load-grunt-tasks')(grunt);

module.exports = function (grunt) {

    var $srcFiles = 'taf_table_view/js/*.js';
    var $testFiles = 'test/*.js';
    var $outputDir = 'test_results'
    var $junitResults = $outputDir + '/junit-test-results.xml';
    var $jasmineSpecRunner = $outputDir + '/_SpecRunner.html';
    var $coverageOutputDir = $outputDir + '/coverage';

    grunt.initConfig({
         pkg: grunt.file.readJSON('package.json'), // Read project settings from package.json
         // Jasmine test
         jasmine: {
             pivotal: {
                 src: [
                     // load dependencies first and make sure angular
                     'taf_table_view/lib/angular/angular.js',
                     'taf_table_view/lib/angular-route/angular-route.js',
                     'taf_table_view/lib/angular-mocks/angular-mocks.js',
                     'taf_table_view/lib/angular-loader/angular-loader.js',
                     'taf_table_view/lib/angular-animate/angular-animate.js',

                     // load taf_table_view and test scripts
                     'taf_table_view/js/*.js',
                     'http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js',
                     'test/*.js'
                 ],
                 options: {
                     specs: $testFiles,
                     outfile: $jasmineSpecRunner,
                     keepRunner: 'true' // keep SpecRunner/outfile file
                 }
             }
         },
         karma: { // documentation: https://github.com/karma-runner/grunt-karma
             plugins : [
                 'karma-chrome-launcher',
                 'karma-firefox-launcher',
                 'karma-phantomjs-launcher',
                 'karma-jasmine',

                 // needed for sonar plugin
                 'karma-junit-reporter',
                 'karma-coverage'
             ],
             continuous: {
                 singleRun: 'true',
                 browsers: [ 'PhantomJS' ]  // PhantomJS
             },
             options: {
                 configFile: 'karma.conf.js',
             },
             unit: {
                 singleRun: true,
                 reporters: ['junit', 'coverage']
             }
             //continuous: {
             //    background: true
             //}
         },
         karma_sonar: {  // documentation: https://github.com/mdasberg/grunt-karma-sonar
             //  http://ahexamples.blogspot.com/2014/03/example-of-jasmine-karma-sonar-grunt.html
             options: {
                 defaultOutputDir: '/sonar/',
                 instance: {
                    //  http://localhost:9000  // http://10.207.120.229:9000
                     hostUrl: 'http://localhost:9000',
                     //  jdbc:h2:tcp://localhost:9092/sonar // jdbc:postgresql://10.207.120.229:5432/sonar
                     jdbcUrl: 'jdbc:h2:tcp://localhost:9092/sonar',
                     login: 'sonar',
                     // sonar
                     password: 'sonar1dev'
                 }
             },
             update_sonar: {
                 project: {  // Project information shown in sonar
                     key: 'taf-table-view',
                     name: 'TAF Table View',
                     version: '0.0.1'
                 }

                 // sources set at runtime
             }
         }
     });

    grunt.registerTask('set-karma-sonar-sources-property', function() {
        var $done = this.async();
        var $phantomjs = require('karma-phantomjs-launcher/node_modules/phantomjs');
        var $spawn = require('child_process').spawn;
        var $phantomUserAgent = $spawn($phantomjs.path,
                                       // phantomjs script to print user agent string
                                       [ 'lib/phantomjs-useragent.js' ]
        );

        /*
         * Construct coverage LCOV file path from PHantomJs'
         * user agent string, then use it to set karmaSonar's
         * sources property.
         */
        $phantomUserAgent.stdout.on('data', function(msg) {
            var $useragent = require('karma/node_modules/useragent');
            var $agent = $useragent.parse(msg);
            // An example of dirName is 'PhantomJS 1.9.7 (Mac OS X)'
            var $dirName = $agent.toAgent() + ' (' + $agent.os + ')';
            var $coverageResults = $coverageOutputDir + '/' + $dirName + '/lcov.info';
            var $sonarSources = makeSonarSourceDirs($srcFiles, $coverageResults);
            var $karmaSonarConfig = 'karmaSonar';
            var $ksConfig = grunt.config($karmaSonarConfig);

            grunt.log.writeln('coverage LCOV file: ' + $coverageResults);
            $ksConfig['your_target']['sources'] = $sonarSources;
            grunt.config($karmaSonarConfig, $ksConfig);

        });

        $phantomUserAgent.on('close', function(exitCode) {
            $done();
        });


        /*
         * Create sonar source object for each directory of source file pattern.
         */
        function makeSonarSourceDirs($filesPattern, $coverageResults) {
            var $path = require('path');
            var $dirs = [];

            grunt.file.expand(
                {
                    filter: function($filePath) {
                        $dirs.push({
                                       path: $path.dirname($filePath),
                                       prefix: '.', // path prefix in lcov.info
                                       coverageReport: $coverageResults,
                                       testReport: $junitResults
                                   });
                    }
                },
                $filesPattern
            );

            return $dirs;
        }
    });

    // Tell Grunt we plan to use these plug-ins
    grunt.loadNpmTasks('grunt-contrib-jasmine');
    grunt.loadNpmTasks('grunt-karma');
    grunt.loadNpmTasks('grunt-karma-sonar');
    grunt.loadNpmTasks('load-grunt-tasks');

    grunt.registerTask('test', [ 'jasmine', 'karma:continuous' ]);
    grunt.registerTask('sonar-only', [ 'set-karma-sonar-sources-property', 'karma_sonar:update_sonar' ]);
    grunt.registerTask('sonar', [ 'test', 'sonar-only' ]);
    grunt.registerTask('default', 'test');

如何从字符串表示中实例化一个字节?

1 个答案:

答案 0 :(得分:3)

这是一个使用正则表达式和parseInt的解决方案。

def parseBytes(s: String): List[Byte] =
  (raw"\b0x([0-9a-f]{2})\b".r
    .findAllMatchIn(s)
    .map(g => Integer.parseInt(g.group(1), 16).toByte)
    .toList)

测试:

scala> parseBytes("0x4e 0x01 0x09 0xff")
0: List[Byte] = List(78, 1, 9, -1)