Beispiel #1
0
// NewLabel returns a new label whos name consists of a prefix followed by
// some digits which make it unique.
//
// Only the first length characters of the prefix are used, and the prefix must
// not end with a digit. If no prefix is given, then the default is used.
//
// Label is initially not initally placed.
func NewLabel(prefix string) *Label {

	l := &Label{
		count:  count,
		length: 5,
		prefix: "label",
	}

	if utf8.RuneCountInString(prefix) > utf8.RuneCountInString(l.prefix) {
		l.prefix = prefix[:utf8.RuneCountInString(prefix)-1]
	}

	lastRune, _ := utf8.DecodeLastRuneInString(l.prefix)

	if prefix == "main" {
		l.name = "main"
		return l
	}

	if unicode.IsDigit(lastRune) {
		log.Fatal("label ended with a digit")
	}

	l.name = l.prefix + strconv.Itoa(l.count)
	count++
	return l
}
Beispiel #2
0
func (c *Client) Event(title string, text string, eo *EventOpts) error {
	var b bytes.Buffer
	fmt.Fprintf(&b, "_e{%d,%d}:%s|%s|t:%s", utf8.RuneCountInString(title),
		utf8.RuneCountInString(text), title, text, eo.AlertType)

	if eo.SourceTypeName != "" {
		fmt.Fprintf(&b, "|s:%s", eo.SourceTypeName)
	}
	if !eo.DateHappened.IsZero() {
		fmt.Fprintf(&b, "|d:%d", eo.DateHappened.Unix())
	}
	if eo.Priority != "" {
		fmt.Fprintf(&b, "|p:%s", eo.Priority)
	}
	if eo.Host != "" {
		fmt.Fprintf(&b, "|h:%s", eo.Host)
	}
	if eo.AggregationKey != "" {
		fmt.Fprintf(&b, "|k:%s", eo.AggregationKey)
	}
	tags := append(c.Tags, eo.Tags...)
	format := "|#%s"
	for _, t := range tags {
		fmt.Fprintf(&b, format, t)
		format = ",%s"
	}

	bytes := b.Bytes()
	if len(bytes) > maxEventBytes {
		return fmt.Errorf("Event '%s' payload is too big (more that 8KB), event discarded", title)
	}
	_, err := c.conn.Write(bytes)
	return err
}
Beispiel #3
0
// DiffToDelta crushes the diff into an encoded string which describes the
// operations required to transform text1 into text2.
// E.g. =3\t-2\t+ing  -> Keep 3 chars, delete 2 chars, insert 'ing'.
// Operations are tab-separated.  Inserted text is escaped using %xx
// notation.
func DiffToDelta(diffs []Diff) string {
	var buf bytes.Buffer
	for _, d := range diffs {
		switch d.Type {
		case DiffInsert:
			buf.WriteString("+")
			buf.WriteString(
				strings.Replace(url.QueryEscape(d.Text), "+", " ", -1),
			)
			buf.WriteString("\t")
			break
		case DiffDelete:
			buf.WriteString("-")
			buf.WriteString(strconv.Itoa(utf8.RuneCountInString(d.Text)))
			buf.WriteString("\t")
			break
		case DiffEqual:
			buf.WriteString("=")
			buf.WriteString(strconv.Itoa(utf8.RuneCountInString(d.Text)))
			buf.WriteString("\t")
			break
		}
	}
	delta := buf.String()
	if len(delta) != 0 {
		// Strip off trailing tab character.
		delta = delta[0 : utf8.RuneCountInString(delta)-1]
		delta = unescaper.Replace(delta)
	}
	return delta
}
Beispiel #4
0
func checkTrim(t string) bool {
	diff := ((utf8.RuneCountInString(t) - len(t)) / 2)
	if utf8.RuneCountInString(t) > (titleCount + diff) {
		return true
	}
	return false
}
func ZipStrByGoroutine(s1, s2 string) string {
	zippedLength := utf8.RuneCountInString(s1) + utf8.RuneCountInString(s2)
	zipped := make([]rune, zippedLength)
	recvCh := make(chan rune)
	switchCh1 := make(chan struct{})
	switchCh2 := make(chan struct{})

	go func() {
		for _, r := range s1 {
			<-switchCh1
			recvCh <- r
			switchCh2 <- struct{}{}
		}
	}()
	go func() {
		for _, r := range s2 {
			<-switchCh2
			recvCh <- r
			switchCh1 <- struct{}{}
		}
	}()

	i := 0
	switchCh1 <- struct{}{}
	for r := range recvCh {
		zipped[i] = r
		i++
		if i == zippedLength {
			break
		}
	}

	return string(zipped)
}
func ValidateLengthOf(resource Resource, attribute string, rule string, chars int) {
	value := reflect.ValueOf(resource).Elem()

	value = reflect.Indirect(value).FieldByName(attribute)

	switch rule {
	case "at least":
		if utf8.RuneCountInString(value.String()) < chars {
			message := fmt.Sprintf("length can't be less than %d characters", chars)

			resource.Errors().Add(attribute, message)
		}
	case "at most":
		if utf8.RuneCountInString(value.String()) > chars {
			message := fmt.Sprintf("length can't be more than %d characters", chars)

			resource.Errors().Add(attribute, message)
		}
	case "exactly":
		if utf8.RuneCountInString(value.String()) != chars {
			message := fmt.Sprintf("length must equal %d characters", chars)

			resource.Errors().Add(attribute, message)
		}
	}
}
Beispiel #7
0
func saveConfig(w io.Writer, obsKeys map[string]string) {
	// find flags pointing to the same variable. We will only write the longest
	// named flag to the config file, the shorthand version is ignored.
	deduped := make(map[flag.Value]flag.Flag)
	flag.VisitAll(func(f *flag.Flag) {
		if cur, ok := deduped[f.Value]; !ok || utf8.RuneCountInString(f.Name) > utf8.RuneCountInString(cur.Name) {
			deduped[f.Value] = *f
		}
	})
	flag.VisitAll(func(f *flag.Flag) {
		if cur, ok := deduped[f.Value]; ok && cur.Name == f.Name {
			_, usage := flag.UnquoteUsage(f)
			usage = strings.Replace(usage, "\n    \t", "\n# ", -1)
			fmt.Fprintf(w, "\n# %s (default %v)\n", usage, f.DefValue)
			fmt.Fprintf(w, "%s=%v\n", f.Name, f.Value.String())
		}
	})

	// if we have obsolete keys left from the old config, preserve them in an
	// additional section at the end of the file
	if obsKeys != nil && len(obsKeys) > 0 {
		fmt.Fprintln(w, "\n\n# The following options are probably deprecated and not used currently!")
		for key, val := range obsKeys {
			fmt.Fprintf(w, "%v=%v\n", key, val)
		}
	}
}
Beispiel #8
0
func DrawLabel(s *svg.SVG, bs string) *BrailleCanvas {
	var c BrailleCanvas

	// TODO: they hard coded
	c.dotSize = 12
	c.pageMarginTop = 3
	c.pageMarginBottom = 3
	c.pageMarginLeft = 3
	c.pageMarginRight = 3
	c.gapCell = 3

	c.canvasW = c.pageMarginLeft + c.pageMarginRight
	c.canvasW += c.dotSize * 2 * utf8.RuneCountInString(bs)
	c.canvasW += c.gapCell*utf8.RuneCountInString(bs) - 1

	c.canvasH = c.pageMarginTop + c.pageMarginBottom
	c.canvasH += c.dotSize * 3

	s.Start(c.canvasW, c.canvasH)

	x := c.pageMarginLeft
	y := c.pageMarginTop
	for _, b := range bs {
		if b&0x2800 != 0x2800 {
			log.Printf("0x%x is not a braille character!\n", b)
			continue
		}
		Draw(s, b, x, y, c.dotSize)
		x += (c.dotSize * 2) + c.gapCell
	}

	return &c
}
Beispiel #9
0
// DiffToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2.
// E.g. =3\t-2\t+ing  -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated.  Inserted text is escaped using %xx notation.
func (dmp *DiffMatchPatch) DiffToDelta(diffs []Diff) string {
	var text bytes.Buffer
	for _, aDiff := range diffs {
		switch aDiff.Type {
		case DiffInsert:
			_, _ = text.WriteString("+")
			_, _ = text.WriteString(strings.Replace(url.QueryEscape(aDiff.Text), "+", " ", -1))
			_, _ = text.WriteString("\t")
			break
		case DiffDelete:
			_, _ = text.WriteString("-")
			_, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
			_, _ = text.WriteString("\t")
			break
		case DiffEqual:
			_, _ = text.WriteString("=")
			_, _ = text.WriteString(strconv.Itoa(utf8.RuneCountInString(aDiff.Text)))
			_, _ = text.WriteString("\t")
			break
		}
	}
	delta := text.String()
	if len(delta) != 0 {
		// Strip off trailing tab character.
		delta = delta[0 : utf8.RuneCountInString(delta)-1]
		delta = unescaper.Replace(delta)
	}
	return delta
}
func AreOneEditAway(input1, input2 string) bool {
	len1 := utf8.RuneCountInString(input1)
	len2 := utf8.RuneCountInString(input2)
	if len1 != len2 && len1-1 != len2 && len2-1 != len1 {
		return false
	}
	if len1 == len2 { // must be one replacement
		var width1, width2 int
		var r1, r2 rune
		diffSeen := false
		for i, j := 0, 0; i < len1 || j < len2; i, j = i+width1, j+width2 {
			r1, width1 = utf8.DecodeRuneInString(input1[i:])
			r2, width2 = utf8.DecodeRuneInString(input2[j:])
			if r1 != r2 {
				if diffSeen {
					return false
				} else {
					diffSeen = true
				}
			}
		}
		return true
	} else if len1-1 == len2 { // input1 must be a removal from input2
		return oneRemovalAway(input2, input1)
	} else { //if len2-1 == len1 { // input2 must be a removal from input1
		return oneRemovalAway(input1, input2)
	}
}
Beispiel #11
0
func (t *PrintableTable) cellValue(col int, value string) string {
	padding := ""

	if col < len(t.headers)-1 {
		var count int

		if utf8.RuneCountInString(value) == len(value) {
			if t.maxRuneCountLengths[col] == t.maxStringLengths[col] {
				count = t.maxRuneCountLengths[col] - utf8.RuneCountInString(Decolorize(value))
			} else {
				count = t.maxRuneCountLengths[col] - len(Decolorize(value))
			}
		} else {
			if t.maxRuneCountLengths[col] == t.maxStringLengths[col] {
				count = t.maxRuneCountLengths[col] - len(Decolorize(value)) + utf8.RuneCountInString(Decolorize(value))
			} else {
				count = t.maxStringLengths[col] - len(Decolorize(value))
			}
		}

		padding = strings.Repeat(" ", count)
	}

	return fmt.Sprintf("%s%s   ", value, padding)
}
Beispiel #12
0
// слово
func (l *Lorem) Worlds(num int) string {

	if num == 0 {
		return ""
	}

	var worlds []string
	result := ""
	k := 0
	for i := 0; i < num; i++ {
		if len(worlds) == 0 || k >= len(worlds) {
			speech := l.Speech(1)
			worlds = strings.Split(speech[:utf8.RuneCountInString(speech)-1], " ")
			k = 0
		}

		result += worlds[k]
		if i < num-1 {
			result += " "
		}

		k++
	}

	if result[utf8.RuneCountInString(result)-1:] == "," {
		result = result[:utf8.RuneCountInString(result)-1]
	}

	return result + ". "
}
Beispiel #13
0
func resetPassword(app *Application, answer *model.SecurityAnswer, token *model.Token, r *http.Request) error {
	ans := r.FormValue("answer")
	pass := r.FormValue("password")
	pass2 := r.FormValue("password2")

	if len(pass) < viper.GetInt("min_passwd_len") || len(pass2) < viper.GetInt("min_passwd_len") {
		return errors.New(fmt.Sprintf("Please set a password at least %d characters in length.", viper.GetInt("min_passwd_len")))
	}

	if pass != pass2 {
		return errors.New("Password do not match. Please confirm your password.")
	}

	if utf8.RuneCountInString(ans) < 2 || utf8.RuneCountInString(ans) > 100 {
		return errors.New("Invalid answer. Must be between 2 and 100 characters long.")
	}

	err := bcrypt.CompareHashAndPassword([]byte(answer.Answer), []byte(ans))
	if err != nil {
		return errors.New("The security answer you provided does not match. Please check that you are entering the correct answer.")
	}

	// Setup password in FreeIPA
	err = setPassword(token.UserName, "", pass)
	if err != nil {
		if ierr, ok := err.(*ipa.ErrPasswordPolicy); ok {
			logrus.WithFields(logrus.Fields{
				"uid":   token.UserName,
				"error": ierr.Error(),
			}).Error("password does not conform to policy")
			return errors.New("Your password is too weak. Please ensure your password includes a number and lower/upper case character")
		}

		if ierr, ok := err.(*ipa.ErrInvalidPassword); ok {
			logrus.WithFields(logrus.Fields{
				"uid":   token.UserName,
				"error": ierr.Error(),
			}).Error("invalid password from FreeIPA")
			return errors.New("Invalid password.")
		}

		logrus.WithFields(logrus.Fields{
			"uid":   token.UserName,
			"error": err.Error(),
		}).Error("failed to set user password in FreeIPA")
		return errors.New("Fatal system error")
	}

	// Destroy token
	err = model.DestroyToken(app.db, token.Token)
	if err != nil {
		logrus.WithFields(logrus.Fields{
			"uid":   token.UserName,
			"error": err.Error(),
		}).Error("failed to remove token from database")
		return errors.New("Fatal system error")
	}

	return nil
}
Beispiel #14
0
func (nv *stringValidater) Valid(source string, opt *FieldOption) (vr *ValidResult) {
	source, vr = nv.baseValidater.Valid(source, opt)
	if vr.IsValid || vr.ErrorMsg != "" {
		return vr
	}
	if opt.Range[0] > 0 && utf8.RuneCountInString(source) < opt.Range[0] {
		if opt.Range[1] > 0 {
			vr.ErrorMsg = strings.Replace(
				getOrDefault(opt.ErrorMsgs, "range", MST_RANGE_LENGTH),
				"{0}", strconv.Itoa(opt.Range[0]), -1)
			vr.ErrorMsg = strings.Replace(vr.ErrorMsg, "{1}", strconv.Itoa(opt.Range[1]), -1)
		} else {
			vr.ErrorMsg = strings.Replace(
				getOrDefault(opt.ErrorMsgs, "min", MSG_MIN_LENGTH), "{0}", strconv.Itoa(opt.Range[0]), -1)
		}
		return vr
	}
	if opt.Range[1] > 0 && utf8.RuneCountInString(source) > opt.Range[1] {
		if opt.Range[0] > 0 {
			vr.ErrorMsg = strings.Replace(
				getOrDefault(opt.ErrorMsgs, "range", MST_RANGE_LENGTH), "{0}", strconv.Itoa(opt.Range[0]), -1)
			vr.ErrorMsg = strings.Replace(vr.ErrorMsg, "{1}", strconv.Itoa(opt.Range[1]), -1)
		} else {
			vr.ErrorMsg = strings.Replace(
				getOrDefault(opt.ErrorMsgs, "max", MSG_MAX_LENGTH), "{0}", strconv.Itoa(opt.Range[1]), -1)
		}
		return vr
	}
	vr.IsValid = true
	vr.CleanValue = source

	return vr
}
Beispiel #15
0
// IsValid validates the user and returns an error if it isn't configured
// correctly.
func (u *User) IsValid() *AppError {

	if len(u.Id) != 26 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.id.app_error", nil, "")
	}

	if u.CreateAt == 0 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.create_at.app_error", nil, "user_id="+u.Id)
	}

	if u.UpdateAt == 0 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.update_at.app_error", nil, "user_id="+u.Id)
	}

	if len(u.TeamId) != 26 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.team_id.app_error", nil, "")
	}

	if !IsValidUsername(u.Username) {
		return NewLocAppError("User.IsValid", "model.user.is_valid.username.app_error", nil, "user_id="+u.Id)
	}

	if len(u.Email) > 128 || len(u.Email) == 0 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.email.app_error", nil, "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.Nickname) > 64 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.nickname.app_error", nil, "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.FirstName) > 64 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.first_name.app_error", nil, "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.LastName) > 64 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.last_name.app_error", nil, "user_id="+u.Id)
	}

	if len(u.Password) > 128 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.pwd.app_error", nil, "user_id="+u.Id)
	}

	if len(u.AuthData) > 128 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.auth_data.app_error", nil, "user_id="+u.Id)
	}

	if len(u.AuthData) > 0 && len(u.AuthService) == 0 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.auth_data_type.app_error", nil, "user_id="+u.Id)
	}

	if len(u.Password) > 0 && len(u.AuthData) > 0 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.auth_data_pwd.app_error", nil, "user_id="+u.Id)
	}

	if len(u.ThemeProps) > 2000 {
		return NewLocAppError("User.IsValid", "model.user.is_valid.theme.app_error", nil, "user_id="+u.Id)
	}

	return nil
}
Beispiel #16
0
func ExtractLogHeader(msg *logmessage.LogMessage, loc *time.Location) (logHeader, coloredLogHeader string) {
	logMsg := msg
	sourceName := logMsg.GetSourceName()
	sourceID := logMsg.GetSourceId()
	t := time.Unix(0, logMsg.GetTimestamp())
	timeFormat := "2006-01-02T15:04:05.00-0700"
	timeString := t.In(loc).Format(timeFormat)

	if sourceID == "" {
		logHeader = fmt.Sprintf("%s [%s]", timeString, sourceName)
	} else {
		logHeader = fmt.Sprintf("%s [%s/%s]", timeString, sourceName, sourceID)
	}

	coloredLogHeader = terminal.LogSysHeaderColor(logHeader)

	// Calculate padding
	longestHeader := fmt.Sprintf("%s  [HEALTH/10] ", timeFormat)
	expectedHeaderLength := utf8.RuneCountInString(longestHeader)
	padding := strings.Repeat(" ", max(0, expectedHeaderLength-utf8.RuneCountInString(logHeader)))

	logHeader = logHeader + padding
	coloredLogHeader = coloredLogHeader + padding

	return
}
Beispiel #17
0
// IsValid validates the user and returns an error if it isn't configured
// correctly.
func (u *User) IsValid() *AppError {

	if len(u.Id) != 26 {
		return NewAppError("User.IsValid", "Invalid user id", "")
	}

	if u.CreateAt == 0 {
		return NewAppError("User.IsValid", "Create at must be a valid time", "user_id="+u.Id)
	}

	if u.UpdateAt == 0 {
		return NewAppError("User.IsValid", "Update at must be a valid time", "user_id="+u.Id)
	}

	if len(u.TeamId) != 26 {
		return NewAppError("User.IsValid", "Invalid team id", "")
	}

	if !IsValidUsername(u.Username) {
		return NewAppError("User.IsValid", "Invalid username", "user_id="+u.Id)
	}

	if len(u.Email) > 128 || len(u.Email) == 0 {
		return NewAppError("User.IsValid", "Invalid email", "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.Nickname) > 64 {
		return NewAppError("User.IsValid", "Invalid nickname", "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.FirstName) > 64 {
		return NewAppError("User.IsValid", "Invalid first name", "user_id="+u.Id)
	}

	if utf8.RuneCountInString(u.LastName) > 64 {
		return NewAppError("User.IsValid", "Invalid last name", "user_id="+u.Id)
	}

	if len(u.Password) > 128 {
		return NewAppError("User.IsValid", "Invalid password", "user_id="+u.Id)
	}

	if len(u.AuthData) > 128 {
		return NewAppError("User.IsValid", "Invalid auth data", "user_id="+u.Id)
	}

	if len(u.AuthData) > 0 && len(u.AuthService) == 0 {
		return NewAppError("User.IsValid", "Invalid user, auth data must be set with auth type", "user_id="+u.Id)
	}

	if len(u.Password) > 0 && len(u.AuthData) > 0 {
		return NewAppError("User.IsValid", "Invalid user, password and auth data cannot both be set", "user_id="+u.Id)
	}

	if len(u.ThemeProps) > 2000 {
		return NewAppError("User.IsValid", "Invalid theme", "user_id="+u.Id)
	}

	return nil
}
Beispiel #18
0
func TestLatticeBuild05(t *testing.T) {

	la := New(dic.SysDic(), nil)
	if la == nil {
		t.Fatal("cannot new a lattice")
	}
	defer la.Free()

	inp := "ポポピポンポコナーノ"
	var b bytes.Buffer
	for i, step := 0, utf8.RuneCountInString(inp); i < maximumUnknownWordLength; i = i + step {
		if _, e := b.WriteString(inp); e != nil {
			t.Fatalf("unexpected error: create the test input, %v", b.String())
		}
	}
	la.Build(b.String())
	for i := range la.list {
		for j := range la.list[i] {
			l := utf8.RuneCountInString(la.list[i][j].Surface)
			if l > maximumUnknownWordLength {
				t.Errorf("too long unknown word, %v", l)
			}
		}
	}
}
Beispiel #19
0
// Takes year/month/day (in a variety of formats) and HH:MM parameters
// Checks if the time is valid and returns error if it is not
// Parses in the location of Santiago
func ReturnTime(d string, t string) (time.Time, error) {
	loc, err := time.LoadLocation("America/Santiago")
	if err != nil {
		return time.Time{}, NewError(err, "Error de servidor", 500)
	}

	layout := returnTimeLayout(d)
	splits := strings.Split(t, ":")
	if len(splits) == 1 {
		if utf8.RuneCountInString(t) == 2 {
			layout += " 15"
		} else if utf8.RuneCountInString(t) == 1 {
			t = "0" + t
			layout += " 15"
		} else if utf8.RuneCountInString(t) == 4 {
			layout += " 1504"
		}
	} else if len(splits) == 2 {
		layout += " 15:04"
	}

	timeVar, err := time.ParseInLocation(layout, d+" "+t, loc)
	if err != nil {
		return time.Time{}, NewError(nil, "Formato de tiempo invalido", 400)
	}

	return timeVar, nil
}
Beispiel #20
0
// ------------------------------------------------------------------
// -  TRANSLATION  --------------------------------------------------
// ------------------------------------------------------------------
func newTranslation(pattern string, replacement string) (instruction, error) {
	rc1 := utf8.RuneCountInString(pattern)
	rc2 := utf8.RuneCountInString(replacement)
	if rc1 != rc2 {
		return nil, fmt.Errorf("Translation 'y' pattern and replacement must be equal length")
	}

	// fill out repls array with alternating patterns and their replacements
	var repls = make([]string, rc1+rc2)
	idx := 0
	for _, ch := range pattern {
		repls[idx] = string(ch)
		idx += 2
	}
	idx = 1
	for _, ch := range replacement {
		repls[idx] = string(ch)
		idx += 2
	}

	stringReplacer := strings.NewReplacer(repls...)

	// now return a custom-made instruction for this translation:
	return func(svm *vm) error {
		svm.pat = stringReplacer.Replace(svm.pat)
		svm.ip++
		return nil
	}, nil
}
Beispiel #21
0
// Normalizes time format to one of two layouts (machine or human readable)
// Checks if the time is valid and returns error if it is not
// Parses in the location of Santiago
func ReturnTimeString(humanReadable bool, d string, t string) (string, string, error) {
	const (
		layoutHuman   = "2/1/2006"
		layoutMachine = "2006-01-02"
	)
	loc, err := time.LoadLocation("America/Santiago")
	if err != nil {
		return "", "", NewError(err, "Error de servidor", 500)
	}

	layout := returnTimeLayout(d)
	splits := strings.Split(t, ":")
	if len(splits) == 1 {
		if utf8.RuneCountInString(t) == 2 {
			layout += " 15"
		} else if utf8.RuneCountInString(t) == 1 {
			t = "0" + t
			layout += " 1"
		} else if utf8.RuneCountInString(t) == 4 {
			layout += " 1504"
		}
	} else if len(splits) == 2 {
		layout += " 15:04"
	}

	timeVar, err := time.ParseInLocation(layout, d+" "+t, loc)
	if err != nil {
		return "", "", NewError(nil, "Formato de tiempo invalido", 400)
	}
	if humanReadable {
		return timeVar.Format(layoutHuman), timeVar.Format("15:04"), nil
	} else {
		return timeVar.Format(layoutMachine), timeVar.Format("15:04"), nil
	}
}
Beispiel #22
0
func (p *Parser) getAlignmentInfo() alignmentInfo {
	ret := alignmentInfo{
		maxLongLen:      0,
		hasShort:        false,
		hasValueName:    false,
		terminalColumns: getTerminalColumns(),
	}

	if ret.terminalColumns <= 0 {
		ret.terminalColumns = 80
	}

	p.EachGroupWithTopLevel(func(index int, grp *Group) {
		for _, info := range grp.Options {
			if info.ShortName != 0 {
				ret.hasShort = true
			}

			lv := utf8.RuneCountInString(info.ValueName)

			if lv != 0 {
				ret.hasValueName = true
			}

			l := utf8.RuneCountInString(info.LongName) + lv

			if l > ret.maxLongLen {
				ret.maxLongLen = l
			}
		}
	})

	return ret
}
Beispiel #23
0
func (t *Table) autoWidth() error {
	//each column
	var wa columns.Columns
	colsvisbile := t.columnsvisible
	for i := range colsvisbile {
		if colsvisbile[i].IsAutoSize() || t.autoSize > 0 {
			colsvisbile[i].MaxLen = len(colsvisbile[i].Caption)

			wa.Add(colsvisbile[i])
		}
	}
	if len(wa) == 0 {
		return nil
	}
	for _, data := range t.Data {
		for i := range wa {
			curval := fmt.Sprintf("%v", data[wa[i].Name])
			curlen := utf8.RuneCountInString(curval)
			if curlen > wa[i].MaxLen {
				wa[i].MaxLen = curlen
			}
		}
	}
	//autosize table
	if t.autoSize > 0 {
		termwidth := t.autoSize - utf8.RuneCountInString(Borders[t.border][BKVertical])*colsvisbile.Len() - utf8.RuneCountInString(Borders[t.border][BKVerticalBorder])*2
		nowwidths := make([]int, colsvisbile.Len())
		allcolswidth := 0
		for i := range colsvisbile {
			if colsvisbile[i].MaxLen > colsvisbile[i].Width || colsvisbile[i].IsAutoSize() {
				nowwidths[i] = colsvisbile[i].MaxLen
			} else {
				nowwidths[i] = colsvisbile[i].Width
			}
			allcolswidth += nowwidths[i]
		}
		//todo: allcolswidth - borders
		twAll := 0
		for i := range colsvisbile {
			colsvisbile[i].MaxLen = int(math.Trunc(float64(termwidth) * (float64(nowwidths[i]) / float64(allcolswidth))))
			twAll += colsvisbile[i].MaxLen
		}
		i := 0
		//distrib mod
		for {
			if twAll >= termwidth || twAll <= 0 {
				break
			}
			if i+1 >= colsvisbile.Len() {
				i = 0
			}
			colsvisbile[i].MaxLen = colsvisbile[i].MaxLen + 1

			twAll = twAll + 1
			i = i + 1
		}
	}
	return nil
}
Beispiel #24
0
// The indices generated by the various extract functions are
// byte offsets. This function calculates charecter/rune offsets
// based on those offsets
func (entities entitiesT) fixIndices(text string) {
	for _, e := range entities {
		start := utf8.RuneCountInString(text[:e.ByteRange.Start])
		e.Range.Start = start
		stop := utf8.RuneCountInString(e.Text)
		e.Range.Stop = start + stop
	}
}
func main() {
	m := "møøse"
	u := "𝔘𝔫𝔦𝔠𝔬𝔡𝔢"
	j := "J̲o̲s̲é̲"
	fmt.Printf("%d %s %x\n", utf8.RuneCountInString(m), m, []rune(m))
	fmt.Printf("%d %s %x\n", utf8.RuneCountInString(u), u, []rune(u))
	fmt.Printf("%d %s %x\n", utf8.RuneCountInString(j), j, []rune(j))
}
Beispiel #26
0
//Ratio allow you to calculate the pourcentage of variance between two strings
//if the two strings are equals the function returns 1.
func Ratio(s1, s2 string) int {
	if s1 == "" || s2 == "" {
		return 0
	}
	len := utf8.RuneCountInString(s1) + utf8.RuneCountInString(s2)
	dist := LevenshteinDistance(processString(s1), processString(s2))
	return int((1 - (float32(dist) / float32(len))) * 100)
}
Beispiel #27
0
func main() {

	for _, arg := range os.Args {
		pair := strings.Split(arg, "=")
		if len(pair) > 2 {
			continue
		}

		switch pair[0] {
		case "--apik":
			WaniKaniApiKey = pair[1]
			if len(WaniKaniApiKey) != 32 {
				WaniKaniApiKey = INCORRECT_API_KEY
				fmt.Printf("Incorrect API key. Please check your input and try again.\n")
			}
		case "--levels":
			Levels = pair[1]
			fmt.Printf("Request kanji only for levels: %s\n", Levels)
		default:
			if len(pair) == 1 && pair[0] != os.Args[0] {
				InputFiles = append(InputFiles, pair[0])
				fmt.Printf("Input file:%s.\n", pair[0])
			}
		}
	}

	var slice []string
	slice = append(slice, ALL_WK_KANJI)

	AllWKKanji, _ := readInputFiles(slice)

	if WaniKaniApiKey != INCORRECT_API_KEY {
		fmt.Print("API key is valid.\n")
		KanjiFromWK = loadWaniKaniData(WaniKaniApiKey)
		fmt.Printf("Your kanji list loaded. You already know %d/%d of WK kanji.\n", utf8.RuneCountInString(KanjiFromWK), utf8.RuneCountInString(AllWKKanji))
	} else {
		return
	}

	KanjiInText, TextLength = readInputFiles(InputFiles)
	UniqKanjiInTexts = uniqueKanjiInString(KanjiInText)
	KanjiPercentageInTexts = float32(utf8.RuneCountInString(KanjiInText)) / float32(TextLength) * 100

	UnknownKanji = kanjiDifference(UniqKanjiInTexts, KanjiFromWK)
	KanjiNotInWK = kanjiDifference(UnknownKanji, AllWKKanji)
	UnknownKanjiInWK = kanjiDifference(UnknownKanji, KanjiNotInWK)
	KnownKanji = kanjiDifference(UniqKanjiInTexts, UnknownKanji)

	UnknownKanjiPercentage = kanjiPercent(UnknownKanji, KanjiInText)
	KanjiNotInWKPercentage = kanjiPercent(KanjiNotInWK, KanjiInText)
	UnknownKanjiInWKPercentage = kanjiPercent(UnknownKanjiInWK, KanjiInText)
	KnownKanjiPercentage = kanjiPercent(KnownKanji, KanjiInText)

	fmt.Printf("All:%d	100.0%%\nKnown: %d	%3.1f%%\nUnknown:%d	%3.1f%%\n   WK: %d	%3.1f%%\n   not WK:%d	%3.1f%%\n", utf8.RuneCountInString(UniqKanjiInTexts), utf8.RuneCountInString(KnownKanji), KnownKanjiPercentage, utf8.RuneCountInString(UnknownKanji), UnknownKanjiPercentage, utf8.RuneCountInString(UnknownKanjiInWK), UnknownKanjiInWKPercentage, utf8.RuneCountInString(KanjiNotInWK), KanjiNotInWKPercentage)

	fmt.Printf("Kanji percent in texts: %3.1f%%\n", KanjiPercentageInTexts)

}
Beispiel #28
0
func (c *cow) balloonText(text string, think bool, maxWidth int) string {
	var first, middle, last, only [2]rune
	if think {
		first = [2]rune{'(', ')'}
		middle = [2]rune{'(', ')'}
		last = [2]rune{'(', ')'}
		only = [2]rune{'(', ')'}
	} else {
		first = [2]rune{'/', '\\'}
		middle = [2]rune{'|', '|'}
		last = [2]rune{'\\', '/'}
		only = [2]rune{'<', '>'}
	}

	text = wordwrap.WrapString(text, uint(maxWidth))

	lines := strings.Split(text, "\n")

	maxWidth = 0
	for _, Line := range lines {
		length := utf8.RuneCountInString(Line)
		if length > maxWidth {
			maxWidth = length
		}
	}

	nbLines := len(lines)
	upper := " "
	lower := " "

	for l := maxWidth + 1; l >= 0; l-- {
		upper += "_"
		lower += "-"
	}

	upper += " "
	lower += " "

	if nbLines > 1 {
		newText := ""
		for index, Line := range lines {
			for spaceCount := maxWidth - utf8.RuneCountInString(Line); spaceCount > 0; spaceCount-- {
				Line += " "
			}
			if index == 0 {
				newText = fmt.Sprintf("%c %s %c\n", first[0], Line, first[1])
			} else if index == nbLines-1 {
				newText += fmt.Sprintf("%c %s %c", last[0], Line, last[1])
			} else {
				newText += fmt.Sprintf("%c %s %c\n", middle[0], Line, middle[1])
			}
		}

		return fmt.Sprintf("%s\n%s\n%s", upper, newText, lower)
	}

	return fmt.Sprintf("%s\n%c %s %c\n%s", upper, only[0], lines[0], only[1], lower)
}
Beispiel #29
0
func main() {
	// count number of characters:
	str1 := "asSASA ddd dsjkdsjs dk"
	fmt.Printf("The number of bytes in string str1 is %d\n", len(str1))
	fmt.Printf("The number of characters in string str1 is %d\n", utf8.RuneCountInString(str1))
	str2 := "asSASA ddd dsjkdsjs こん dk"
	fmt.Printf("The number of bytes in string str2 is %d\n", len(str2))
	fmt.Printf("The number of characters in string str2 is %d", utf8.RuneCountInString(str2))
}
Beispiel #30
0
func longestTLDSuffix(domain string) string {
	longestTld := ""
	for tld := range TLDWhoisServers {
		if strings.HasSuffix(domain, "."+tld) && utf8.RuneCountInString(tld) > utf8.RuneCountInString(longestTld) {
			longestTld = tld
		}
	}
	return longestTld
}