github.com/euank/go@v0.0.0-20160829210321-495514729181/src/cmd/link/internal/ld/util.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package ld
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/binary"
    10  	"os"
    11  	"strings"
    12  	"time"
    13  )
    14  
    15  func cstring(x []byte) string {
    16  	i := bytes.IndexByte(x, '\x00')
    17  	if i >= 0 {
    18  		x = x[:i]
    19  	}
    20  	return string(x)
    21  }
    22  
    23  func tokenize(s string) []string {
    24  	var f []string
    25  	for {
    26  		s = strings.TrimLeft(s, " \t\r\n")
    27  		if s == "" {
    28  			break
    29  		}
    30  		quote := false
    31  		i := 0
    32  		for ; i < len(s); i++ {
    33  			if s[i] == '\'' {
    34  				if quote && i+1 < len(s) && s[i+1] == '\'' {
    35  					i++
    36  					continue
    37  				}
    38  				quote = !quote
    39  			}
    40  			if !quote && (s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n') {
    41  				break
    42  			}
    43  		}
    44  		next := s[:i]
    45  		s = s[i:]
    46  		if strings.Contains(next, "'") {
    47  			var buf []byte
    48  			quote := false
    49  			for i := 0; i < len(next); i++ {
    50  				if next[i] == '\'' {
    51  					if quote && i+1 < len(next) && next[i+1] == '\'' {
    52  						i++
    53  						buf = append(buf, '\'')
    54  					}
    55  					quote = !quote
    56  					continue
    57  				}
    58  				buf = append(buf, next[i])
    59  			}
    60  			next = string(buf)
    61  		}
    62  		f = append(f, next)
    63  	}
    64  	return f
    65  }
    66  
    67  var atExitFuncs []func()
    68  
    69  func AtExit(f func()) {
    70  	atExitFuncs = append(atExitFuncs, f)
    71  }
    72  
    73  func Exit(code int) {
    74  	for i := len(atExitFuncs) - 1; i >= 0; i-- {
    75  		f := atExitFuncs[i]
    76  		atExitFuncs = atExitFuncs[:i]
    77  		f()
    78  	}
    79  	os.Exit(code)
    80  }
    81  
    82  func artrim(x []byte) string {
    83  	i := 0
    84  	j := len(x)
    85  	for i < len(x) && x[i] == ' ' {
    86  		i++
    87  	}
    88  	for j > i && x[j-1] == ' ' {
    89  		j--
    90  	}
    91  	return string(x[i:j])
    92  }
    93  
    94  func stringtouint32(x []uint32, s string) {
    95  	for i := 0; len(s) > 0; i++ {
    96  		var buf [4]byte
    97  		s = s[copy(buf[:], s):]
    98  		x[i] = binary.LittleEndian.Uint32(buf[:])
    99  	}
   100  }
   101  
   102  var start = time.Now()
   103  
   104  func elapsed() float64 {
   105  	return time.Since(start).Seconds()
   106  }